648 words
3 min read
Visual companion
Python
Type and operator map
Python Week 1: the first filter for runtime behavior
View
Revision summary
What this note is really saying
Short form
# Programming in Python · Week 6 — Lists and tuples Deep study for Quiz 2 week 6. Lists are mutable sequences; tuples are immutable — choose based on whether data should change.

Programming in Python · Week 6 — Lists and tuples
Deep study for Quiz 2 week 6. Lists are mutable sequences; tuples are immutable — choose based on whether data should change.
Week map
List creation → indexing/slicing → mutation methods → traversal → tuple immutability → list vs tuple choice.
List notation
lst = [1, 2, 3]→ ordered mutable sequence → elements accessible by index.lst[i]→ element at indexi→ 0-based →lst[0]is first.lst[-1]→ last element → negative index counts from end.lst[a:b]→ slice fromainclusive tobexclusive →lst[1:3]gives two elements.len(lst)→ number of elements.
Mini-example:
pythoncolors = ["red", "green", "blue"] colors[1] = "yellow" # mutable colors.append("black") # [red, yellow, blue, black]
Common list methods
.append(x)→ add one item at end..insert(i, x)→ insert at indexi, shift right..pop()→ remove and return last;.pop(i)at index..remove(x)→ remove first occurrence of valuex.lst + other→ concatenate → new list.lst * n→ repeat listntimes.
Trap:
append adds one element; extend adds each element of iterable.Tuple notation
t = (1, 2, 3)→ ordered immutable sequence → cannot reassign elements.t = (42,)→ single-element tuple needs trailing comma.()→ empty tuple.- Tuples useful for fixed records:
(name, score), coordinates(x, y).
Mini-example:
pythonpoint = (3, 4) x, y = point # unpacking # point[0] = 5 # TypeError — immutable
List vs tuple
| Feature | List | Tuple |
|---|---|---|
| Mutability | Yes | No |
| Syntax | [ ] | ( ) |
| Use case | Growing/changing data | Fixed bundles |
Pattern families
Easy — Index and slice
Access first, last, middle. Slice subsequence. Predict result of
lst[::2] (every second element).Medium — Mutate and traverse
Append, insert, pop in trace problems. Loop with
for x in lst or index for i in range(len(lst)). Build new list by comprehension or loop.Hard — Nested lists and aliasing
List of lists — inner lists are references.
a = b aliases same list; a = b[:] shallow copy. Modify inner list through one alias affects other.Worked mini-examples
Example 1 — Slice.
pythonnums = [10, 20, 30, 40, 50] nums[1:4] # [20, 30, 40] nums[-2:] # [40, 50]
Example 2 — append vs insert.
pythona = [1, 2] a.append([3]) # [1, 2, [3]] b = [1, 2] b.insert(1, 99) # [1, 99, 2]
Example 3 — Traverse and transform.
pythonvals = [1, 2, 3] doubled = [] for v in vals: doubled.append(v * 2) # [2, 4, 6]
Example 4 — Tuple unpack.
pythonpair = ("Alice", 92) name, score = pair
Example 5 — Aliasing trap.
pythonrow = [0, 0] grid = [row, row] grid[0][0] = 1 # grid is [1, 0], [1, 0](/courses/deep/notes/1%2C%200%5D%2C%20%5B1%2C%200) — both rows share row
Traps
t = (5)is int 5, not tuple — need(5,).append([x])nests list;extend([x])adds element.- Slice copy is shallow — inner mutable objects still shared.
- Modifying list while iterating forward can skip elements.
lst.sort()sorts in place;sorted(lst)returns new list.
Diagnostic (try yourself)
-
What is
["a", "b", "c"][1]? What is[-1]? -
After
x = [1, 2]; x.append(3); x.insert(0, 0), what isx? -
Can you change
t[0]ift = (10, 20)? Why or why not? -
What does
a = [1, 2] + [3]produce? How is it different froma.append(3)starting from[1, 2]? -
row = [0, 0]; m = [row, row]; m[1][1] = 9. What ism[0]?