add
change
remove
nums = [10, 20, 30]
len = 3
- Ordered & Changeable: Edit items in-place anytime
- Dynamic & Heterogeneous: Grows freely; mixes types
my_list = [10, "py", 3.5]
empty_list = []
Real-Life:
Shopping Cart
Playlist
To-Do List
append(x)Add at end
insert(i, x)Add at index i
extend(seq)Merge items
pop([i])Remove & return
remove(x)Delete first x
sort()Sort ascending
reverse()Flip list order in-place
TUPLE RULES & PACKING
Must-Know CBSE
-
Single-Item Rule:
single = (5,)
comma needed! (5) is an int
-
Packing & Unpacking:
x, y = (4, 9)
→ x=4, y=9
-
Hashable Dict Keys:
loc[(28, 77)] = "Delhi"
TypeError: Tuples are locked!
'tuple' does not support item assignment
t = (10, 20, 30)
- Ordered & Unchangeable: Locked after creation (immutable)
- Fixed Size & Faster: Uses less RAM; quicker iteration
- Hashable: Safe for dictionary keys & set elements
my_tuple = (10, 20, 30)
single = (5,) # single-item tuple
t[0] → 10 · t[1:3] → (20, 30)
t.count(20) · t.index(30)
Real-Life:
GPS Coords
Date of Birth
RGB Colors
INDEXING & SLICING LAB
Works identically on Lists [ ] & Tuples ( )
Result
seq[1:3] → [20, 30]
| Feature |
Lists [ ] |
Tuples ( ) |
| Mutability |
Mutable (changeable in-place) |
Immutable (locked after creation) |
| Syntax |
Square brackets [10, 20] |
Parentheses (10, 20) · single (5,) |
| Memory |
More memory (dynamic buffer) |
Less memory (compact fixed block) |
| Speed |
Slightly slower iteration |
Faster iteration & lookup |
| Methods |
Many (append, pop, sort…) |
Only 2 (count(), index()) |
| Dict Key Support |
No (unhashable → TypeError) |
Yes (hashable with immutable items) |
| Use Case |
Shopping cart, playlist, to-do list |
GPS coords, date of birth, RGB colors |