Python
Difference Between List, Tuple, and Dictionary in Python, CBSE Class 11/12
Complete comparison of List, Tuple, and Dictionary in Python with examples. CBSE Class 11 and 12 exam-ready comparison table and code.
This is one of the most frequently asked questions in CBSE Class 11 and 12 Computer Science exams. Understanding the differences between List, Tuple, and Dictionary is essential for both theory and practical questions.
Quick Overview
- List - An ordered, mutable collection. Uses square brackets
[]. - Tuple - An ordered, immutable collection. Uses parentheses
(). - Dictionary - An unordered collection of key-value pairs. Uses curly braces
{}.
Complete Comparison Table
| Feature | List | Tuple | Dictionary |
|---|---|---|---|
| Syntax | [1, 2, 3] |
(1, 2, 3) |
{"a": 1, "b": 2} |
| Mutable | Yes | No | Yes |
| Ordered | Yes | Yes | Yes (Python 3.7+) |
| Duplicates | Allowed | Allowed | Keys must be unique |
| Indexing | Yes (a[0]) |
Yes (a[0]) |
By key (a["name"]) |
| Slicing | Yes | Yes | No |
| Brackets | Square [] |
Round () |
Curly {} |
| Empty creation | a = [] |
a = () |
a = {} |
| Speed | Slower | Faster | Fastest for lookup |
| Use case | Collection that changes | Fixed data | Key-value mapping |
| Nesting | Yes | Yes | Yes |
| Methods | Many (append, remove, etc.) | Few (count, index) | Many (keys, values, etc.) |
Creating Each Type
List
# Different ways to create a list
marks = [85, 92, 67, 78]
names = ["Aman", "Priya", "Rahul"]
mixed = [1, "hello", 3.14, True]
empty = []
Tuple
# Different ways to create a tuple
marks = (85, 92, 67, 78)
single = (5,) # Comma is required for single element
names = ("Aman", "Priya", "Rahul")
empty = ()
Important: A single-element tuple needs a trailing comma. Without it, Python treats it as a regular value in parentheses.
a = (5) # This is int, NOT a tuple
b = (5,) # This is a tuple
print(type(a)) # <class 'int'>
print(type(b)) # <class 'tuple'>
Dictionary
# Different ways to create a dictionary
student = {"name": "Aman", "marks": 85, "city": "Delhi"}
empty = {}
Accessing Elements
List, By Index
marks = [85, 92, 67, 78]
print(marks[0]) # 85
print(marks[-1]) # 78
print(marks[1:3]) # [92, 67]
Tuple, By Index
marks = (85, 92, 67, 78)
print(marks[0]) # 85
print(marks[-1]) # 78
print(marks[1:3]) # (92, 67)
Dictionary, By Key
student = {"name": "Aman", "marks": 85, "city": "Delhi"}
print(student["name"]) # Aman
print(student.get("marks")) # 85
print(student.get("age", 0)) # 0 (default if key not found)
Modifying Elements
List, Mutable (Can Change)
marks = [85, 92, 67, 78]
marks[0] = 90 # Change first element
marks.append(88) # Add at end
marks.insert(1, 75) # Insert at index 1
marks.remove(67) # Remove first occurrence of 67
marks.pop() # Remove and return last element
print(marks)
[90, 75, 92, 78]
Tuple, Immutable (Cannot Change)
marks = (85, 92, 67, 78)
# marks[0] = 90 # ERROR! TypeError: 'tuple' does not support item assignment
Once a tuple is created, you cannot add, remove, or change any element.
Dictionary, Mutable (Can Change)
student = {"name": "Aman", "marks": 85}
student["marks"] = 90 # Update existing key
student["city"] = "Delhi" # Add new key-value pair
del student["city"] # Delete a key-value pair
popped = student.pop("marks") # Remove and return value
print(student)
{'name': 'Aman'}
Important Methods
List Methods
marks = [85, 67, 92, 78]
marks.append(88) # Add at end -> [85, 67, 92, 78, 88]
marks.insert(1, 70) # Insert at position -> [85, 70, 67, 92, 78, 88]
marks.remove(67) # Remove value -> [85, 70, 92, 78, 88]
marks.pop(0) # Remove at index -> [70, 92, 78, 88]
marks.sort() # Sort -> [70, 78, 88, 92]
marks.reverse() # Reverse -> [92, 88, 78, 70]
marks.count(88) # Count occurrences -> 1
marks.index(78) # Find index -> 2
length = len(marks) # Length -> 4
Tuple Methods (Only 2)
marks = (85, 67, 92, 67, 78)
marks.count(67) # 2 (how many times 67 appears)
marks.index(92) # 2 (index of first occurrence of 92)
length = len(marks) # 5
Tuples have only count() and index() methods. This is a popular exam question.
Dictionary Methods
student = {"name": "Aman", "marks": 85, "city": "Delhi"}
student.keys() # dict_keys(['name', 'marks', 'city'])
student.values() # dict_values(['Aman', 85, 'Delhi'])
student.items() # dict_items([('name', 'Aman'), ('marks', 85), ('city', 'Delhi')])
student.get("name") # 'Aman'
student.update({"marks": 90, "grade": "A"}) # Update/add multiple
student.pop("city") # Remove and return value
student.clear() # Remove all items
Looping Through Each Type
Looping Through a List
marks = [85, 92, 67, 78]
for m in marks:
print(m, end=" ")
85 92 67 78
Looping Through a Tuple
marks = (85, 92, 67, 78)
for m in marks:
print(m, end=" ")
85 92 67 78
Looping Through a Dictionary
student = {"name": "Aman", "marks": 85, "city": "Delhi"}
# Loop through keys
for key in student:
print(key, ":", student[key])
# Loop through key-value pairs
for key, value in student.items():
print(key, "->", value)
name -> Aman
marks -> 85
city -> Delhi
Nesting
List of Lists
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
print(matrix[1][2]) # 6
Dictionary of Dictionaries
students = {
1: {"name": "Aman", "marks": 85},
2: {"name": "Priya", "marks": 92}
}
print(students[1]["name"]) # Aman
List of Dictionaries
records = [
{"name": "Aman", "marks": 85},
{"name": "Priya", "marks": 92}
]
print(records[0]["name"]) # Aman
When to Use Which?
| Situation | Use |
|---|---|
| Marks of students that may change | List |
| Days of the week (fixed data) | Tuple |
| Student details (name, roll, marks) | Dictionary |
| Coordinates (x, y) | Tuple |
| Shopping cart items | List |
| Configuration settings | Dictionary |
| Database records | List of Dictionaries |
| Function returning multiple values | Tuple |
Common Exam Questions
Q1: What is the difference between a list and a tuple?
A list is mutable (can be changed after creation), while a tuple is immutable (cannot be changed). Lists use square brackets [] and tuples use parentheses (). Lists have more built-in methods than tuples. Tuples are faster than lists.
Q2: Can a dictionary have duplicate keys?
No. Dictionary keys must be unique. If you assign a value to an existing key, the old value is overwritten.
d = {"a": 1, "b": 2, "a": 3}
print(d) # {'a': 3, 'b': 2}
Q3: Can a list be a dictionary key?
No. Dictionary keys must be immutable. Lists are mutable, so they cannot be used as keys. Tuples can be used as dictionary keys because they are immutable.
# Valid
d = {(1, 2): "point"}
# Invalid - will cause TypeError
# d = {[1, 2]: "point"}
Q4: Write the output:
t = (10, 20, 30, 40, 50)
print(t[1:4])
print(t[-2:])
print(t[::-1])
(20, 30, 40)
(40, 50)
(50, 40, 30, 20, 10)
Q5: What is the output of len() for each?
L = [1, 2, [3, 4], 5]
T = (1, 2, (3, 4), 5)
D = {"a": 1, "b": 2, "c": 3}
print(len(L)) # 4 (nested list counts as 1 element)
print(len(T)) # 4
print(len(D)) # 3 (counts key-value pairs)
Memory Tip
Remember LTD (like a company, Limited):
- List = Locked brackets
[], Lots of methods, Liberal (mutable) - Tuple = round brackets
(), Two methods only, Tight (immutable) - Dictionary = curly braces
{}, Data in pairs, Dynamic (mutable)
Understanding these three data structures thoroughly will help you answer both theory and coding questions confidently in your CBSE exam.
Want to learn more?
Explore free chapter-wise notes with quizzes and code playground
Prefer watching over reading?
Subscribe for free.