Programming in Python · Week 6 — Basic collections
1172 words
6 min read
2026-08-16T00:00:00.000Z
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
lists, tuples, mutability, slicing — concepts, pattern families, and traps for Quiz 2 week 6. # Week 6 — basic collections > **Quiz 2 scope:** Weeks 1–8 per IITM May 2026 foundation courses.

Week 6 — basic collections
Quiz 2 scope: Weeks 1–8 per IITM May 2026 foundation courses. Source baseline: IITM BS admissions important-dates calendar · May 2026 cycle. Times on assessments are operational conventions — verify hall ticket.
Part of the Quiz 2 prep system%20%C2%B7%20%5BWeeks%201%E2%80%938%20index%5D(.%2Fmay-2026-python-quiz-2-weeks-1-8-prep) · Pattern atlas · Formula chains.
Week map
Sequence types → indexing → slice → mutate list not tuple
Classify → Represent → Execute → Trap-check
- Recognize: Ask: What does
seq[-2]return? - Procedure: Last element is index -1. Slice [1:4] takes indices 1,2,3. Step 2 skips every other. Empty slice when start >= end with positive step.
- Variations / traps: Watch for: IndexError on len(seq) or -len-1.
Formula chain (compressed)
list mutable vs tuple immutable → indexing/slicing → append vs +.
- List literal —
[a, b, c]— ordered, mutable sequence - Tuple literal —
(a, b, c)— ordered, immutable - Index / slice —
L[i], L[a:b:c]— 0-based, stop exclusive - append —
L.append(x)— mutate in place - Concatenate —
L + M → new list— does not mutate L
Open interactive formula desk · Week 6 tab.
Deep study
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/may26-python/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]?
ChatGPT prep archive
Archived import for extra depth — complements the notes above, not official IITM material.
Core concepts
- List []: ordered, mutable; tuple (): ordered, immutable.
- Indexing: 0-based; negative indices from end; seq[i] one element.
- Slicing: seq[start:end:step] copies subsequence; end exclusive.
- Methods: append, pop; + concatenates sequences.
Notation & vocabulary
| Operation | List | Tuple |
|---|---|---|
| Mutate | yes | no |
a[i] | read/write | read only |
| Slice | new list | new tuple |
Pattern families
Easy — Index and slice
Last element is index -1. Slice [1:4] takes indices 1,2,3. Step 2 skips every other. Empty slice when start >= end with positive step.
Medium — Mutate vs share
Assignment copies reference for lists—two names can point to same list. Slicing often creates new sequence. Tuple assignment unpacks length must match.
Hard — List algorithms trace
append grows end; pop removes indexed item. Track length inside loop. Slicing mid-loop uses updated list state.
Drill these on the pattern atlas — filter to week 6.
Traps
- IndexError on len(seq) or -len-1.
- Confusing slice copy with alias mutation.
- Using tuple where list needed for append.
- Step 0 in slice is invalid.
Retrieval prompts
- What does
seq[-2]return? - Can you append to a tuple?
- Difference between
a = bfor lists vsa = b[:]?
Practice loop
- Read Deep study (if present) or core concepts once.
- Recite the formula chain without looking.
- Open one easy pattern on the interactive atlas for week 6.
- Attempt without solutions; mark studied after an honest try.
- Say one trap aloud before closing the tab.