Lists vs Tuples vs Dictionaries in Python — CBSE AI Students Guide

Three data structures. Three different jobs. Most students learn lists in Class 9 and never fully understand when to switch to a tuple or a dictionary. This guide fixes that — with clear comparisons, working programs, and the exact context CBSE expects you to know.

What You’ll Learn

  • What lists, tuples, and dictionaries are — and the key differences
  • When to use each one and why it matters
  • All essential operations with code and expected output
  • How these structures appear in CBSE AI programs across grades

The Quick Answer — When to Use Which

Before the detail, here is the one-line rule for each:

StructureUse WhenMutable?Ordered?Key-Value?
ListYou have a collection that will change✅ Yes✅ Yes❌ No
TupleYou have fixed data that must not change❌ No✅ Yes❌ No
DictionaryYou need to look up values by a label✅ Yes✅ Yes (Python 3.7+)✅ Yes

Mutable means you can add, remove, or change items after creation. Immutable (tuples) means once created, the data cannot be changed.


Part 1 — Lists

A list stores multiple values in a single variable. Items are enclosed in square brackets [ ] and separated by commas. Lists are ordered (items stay in the order you add them) and mutable (you can change them).

Creating and Accessing Lists

python

# Program to create a list and access its elements

students = ["Arjun", "Priya", "Kiran", "Meena", "Rohan"]

print("Full list         :", students)
print("First element     :", students[0])       # index 0 = first
print("Last element      :", students[-1])      # -1 = last
print("Second to fourth  :", students[1:4])     # slicing
print("Length of list    :", len(students))

Expected Output:

Full list         : ['Arjun', 'Priya', 'Kiran', 'Meena', 'Rohan']
First element     : Arjun
Last element      : Rohan
Second to fourth  : ['Priya', 'Kiran', 'Meena']
Length of list    : 5

Modifying Lists

python

# Program to demonstrate list modification operations

marks = [72, 85, 91, 68, 78]

# Add to end
marks.append(95)
print("After append(95)  :", marks)

# Insert at specific position
marks.insert(2, 88)
print("After insert(2,88):", marks)

# Remove by value
marks.remove(68)
print("After remove(68)  :", marks)

# Remove by index
marks.pop(0)
print("After pop(0)      :", marks)

# Sort ascending
marks.sort()
print("After sort()      :", marks)

# Reverse
marks.reverse()
print("After reverse()   :", marks)

Expected Output:

After append(95)  : [72, 85, 91, 68, 78, 95]
After insert(2,88): [72, 85, 88, 91, 68, 78, 95]
After remove(68)  : [72, 85, 88, 91, 78, 95]
After pop(0)      : [85, 88, 91, 78, 95]
After sort()      : [78, 85, 88, 91, 95]
After reverse()   : [95, 91, 88, 85, 78]

Iterating Over a List

python

# Program to iterate through a list using a for loop

subjects = ["Math", "Science", "AI", "English", "Hindi"]

print("Subjects enrolled:")
for subject in subjects:
    print(" -", subject)

# List with index using enumerate
print("\nWith index:")
for i, subject in enumerate(subjects):
    print(f"  {i+1}. {subject}")

Expected Output:

Subjects enrolled:
 - Math
 - Science
 - AI
 - English
 - Hindi

With index:
  1. Math
  2. Science
  3. AI
  4. English
  5. Hindi

Part 2 — Tuples

A tuple stores multiple values like a list, but uses round brackets ( ) and is immutable — you cannot add, remove, or change items after creation.

When to Use a Tuple

Use a tuple when the data is fixed and should not change. Good examples: months of the year, days of the week, coordinates (latitude, longitude), RGB colour values, CBSE grade boundaries.

python

# Program to create and use a tuple

# Fixed data — days of the week, should never change
days = ("Monday", "Tuesday", "Wednesday", "Thursday",
        "Friday", "Saturday", "Sunday")

print("Days tuple   :", days)
print("First day    :", days[0])
print("Last day     :", days[-1])
print("Weekdays     :", days[0:5])
print("Length       :", len(days))
print("Is 'Friday' in tuple?", "Friday" in days)

Expected Output:

Days tuple   : ('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday')
First day    : Monday
Last day     : Sunday
Weekdays     : ('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday')
Length       : 7
Is 'Friday' in tuple? True

Tuple Unpacking

python

# Program to demonstrate tuple unpacking

# Coordinates of a city — immutable, fixed data
delhi_coords = (28.6139, 77.2090)

latitude, longitude = delhi_coords     # unpacking
print("City     : New Delhi")
print("Latitude :", latitude)
print("Longitude:", longitude)

# Functions often return tuples
def get_stats(data):
    return min(data), max(data), sum(data) / len(data)

marks = [72, 85, 91, 68, 78]
low, high, avg = get_stats(marks)
print(f"\nMin: {low}, Max: {high}, Avg: {avg:.2f}")

Expected Output:

City     : New Delhi
Latitude : 28.6139
Longitude: 77.209

Min: 68, Max: 91, Avg: 78.80

Trying to Modify a Tuple (What Happens)

python

# Demonstrating that tuples are immutable

grades = ("A", "B", "C", "D", "F")
grades[0] = "A+"    # TypeError: 'tuple' object does not support item assignment

This is intentional — if you try to change a tuple, Python raises a TypeError. That protection is exactly why you use tuples for fixed data.


Part 3 — Dictionaries

A dictionary stores data as key-value pairs. Each key is unique and maps to a value. Use curly brackets { } with a colon separating key and value: {"key": value}.

Think of it like a real dictionary: you look up a word (the key) to find its meaning (the value).

Creating and Accessing Dictionaries

python

# Program to create a student record using a dictionary

student = {
    "name"      : "Arjun Sharma",
    "class"     : "XI",
    "marks"     : 88,
    "city"      : "Delhi",
    "passed"    : True
}

print("Full dictionary:", student)
print("Name  :", student["name"])
print("Marks :", student["marks"])
print("City  :", student["city"])

# .get() is safer — returns None if key doesn't exist
print("Phone :", student.get("phone", "Not provided"))

Expected Output:

Full dictionary: {'name': 'Arjun Sharma', 'class': 'XI', 'marks': 88, 'city': 'Delhi', 'passed': True}
Name  : Arjun Sharma
Marks : 88
City  : Delhi
Phone : Not provided

Modifying Dictionaries

python

# Program to modify dictionary entries

student = {"name": "Priya", "marks": 85, "grade": "B"}

# Update existing value
student["marks"] = 92
student["grade"] = "A"

# Add new key-value pair
student["city"] = "Mumbai"

# Remove a key
del student["grade"]

# All keys and values
print("Keys  :", list(student.keys()))
print("Values:", list(student.values()))
print("Items :", list(student.items()))
print("Updated student:", student)

Expected Output:

Keys  : ['name', 'marks', 'city']
Values: ['Priya', 92, 'Mumbai']
Items : [('name', 'Priya'), ('marks', 92), ('city', 'Mumbai')]
Updated student: {'name': 'Priya', 'marks': 92, 'city': 'Mumbai'}

Iterating Over a Dictionary

python

# Program to iterate through a dictionary

subject_marks = {
    "Mathematics" : 88,
    "Science"     : 92,
    "AI"          : 95,
    "English"     : 79,
    "Hindi"       : 85
}

print("Subject-wise marks:")
for subject, marks in subject_marks.items():
    print(f"  {subject:15}: {marks}")

total = sum(subject_marks.values())
average = total / len(subject_marks)
print(f"\nTotal  : {total}")
print(f"Average: {average:.1f}")

Expected Output:

Subject-wise marks:
  Mathematics    : 88
  Science        : 92
  AI             : 95
  English        : 79
  Hindi          : 85

Total  : 439
Average: 87.8

List vs Tuple vs Dictionary — Side-by-Side

python

# Demonstrating all three in one program

# List — ordered, mutable, use for collections that change
city_list = ["Delhi", "Mumbai", "Bengaluru"]
city_list.append("Chennai")        # ✅ works

# Tuple — ordered, immutable, use for fixed data
rgb_red = (255, 0, 0)              # RGB value for red — should never change
# rgb_red[0] = 200                 # ❌ would raise TypeError

# Dictionary — key-value, use for labelled data lookups
student_info = {
    "name"  : "Arjun",
    "marks" : 88,
    "grade" : "B"
}
print("Grade:", student_info["grade"])   # ✅ instant lookup by label

Quick Revision Box

OperationListTupleDictionary
Create[1, 2, 3](1, 2, 3){"a": 1}
Accesslst[0]tup[0]d["key"]
Modifylst[0] = 10❌ Not allowedd["key"] = 10
Add itemlst.append(x)❌ Not allowedd["new"] = x
Remove itemlst.remove(x)❌ Not alloweddel d["key"]
Lengthlen(lst)len(tup)len(d)
Loopfor x in lstfor x in tupfor k,v in d.items()
Check membershipx in lstx in tupk in d

Practice Questions

Q1 (2 marks): Write a Python program to create a list [10, 20, 30, 40], add the elements [14, 15, 12] using extend(), sort the final list in ascending order, and print it.

Model Answer:

python

List_1 = [10, 20, 30, 40]
List_1.extend([14, 15, 12])
List_1.sort()
print(List_1)

Output: [10, 12, 14, 15, 20, 30, 40]


Q2 (MCQ): Which of the following will raise a TypeError in Python?

a) my_list = [1, 2, 3]; my_list[0] = 10 b) my_tuple = (1, 2, 3); my_tuple[0] = 10 c) my_dict = {"a": 1}; my_dict["a"] = 10 d) my_list = [1, 2, 3]; my_list.append(4)

Answer: b) — Tuples are immutable. Attempting to assign a value to a tuple index raises TypeError: 'tuple' object does not support item assignment.


Frequently Asked Questions

Q1: Can a list contain different data types? Yes — Python lists can hold integers, floats, strings, booleans, and even other lists in the same list: mixed = [1, "hello", 3.14, True, [5, 6]]. This flexibility is one of Python’s strengths. Dictionaries can similarly hold values of any type.

Q2: Can a dictionary have the same key twice? No — dictionary keys must be unique. If you assign a value to a key that already exists, the old value is overwritten: d = {"name": "Arjun"}; d["name"] = "Priya"d["name"] is now "Priya". Values can repeat; keys cannot.

Q3: Is a tuple always faster than a list? Yes — tuples are slightly faster to create and access than lists because Python knows they will never change. For CBSE practicals the difference is not noticeable, but it is a valid Viva answer for why you might prefer a tuple for fixed data like coordinates or grade categories.