Lesson 01
Collections Deep Dive — Overview
Choose the right Python collection: list, tuple, set, frozenset, or dict. Compare order, duplicates, mutability, and indexing in one roadmap.
What you will learn
| Topic | Goal |
|---|---|
| Three families | Sequence, set, and mapping containers |
| Decision rules | Order, duplicates, mutability, indexing |
| Method index | All pre-defined functions for list, tuple, set, frozenset, dict |
| Deep dive path | Full examples, nesting, and practice programs |
You already met each type in Data Types and Mutable & Immutable. This module goes deeper: pre-defined methods, nesting rules, and interview-ready patterns.
Complete learning journey
Reading path: List → Tuple → Set → Frozenset → Dictionary → Advanced Practice
Progression: Beginner → Intermediate → Interview ready
Three collection families
| Family | Types | Access pattern |
|---|---|---|
| Sequence (list category) | list, tuple |
Index and slice |
| Set category | set, frozenset |
Membership — no index |
| Mapping | dict |
Key → value |
Quick comparison
| Type | Ordered | Duplicates | Mutable | Index / slice |
|---|---|---|---|---|
list |
Yes | Yes | Yes | Yes |
tuple |
Yes | Yes | No | Yes |
set |
No* | No | Yes | No |
frozenset |
No* | No | No | No |
dict |
Yes (3.7+) | Keys unique | Yes | By key |
*Insertion order may appear in recent Python versions, but sets are not sequence types — do not rely on s[0].
When to use which
| Need | Choose |
|---|---|
| Grow/shrink a ordered collection | list |
| Fixed record that should not rebind | tuple |
| Unique values, membership tests | set |
| Hashable set for keys or nested sets | frozenset |
| Labelled fields (key → value) | dict |
Module lessons — files 02–15
Each lesson below maps to one file in content/python/modules/collections/. MahiDev uses NN-slug.md naming; the slug (without order prefix) is the URL path.
Files 02–05 · List (blue)
| # | Lesson | What you learn | Key methods | Mini project | Open |
|---|---|---|---|---|---|
| 02 | Add & Remove | Grow and shrink lists in place | append, insert, remove, pop, clear |
Student attendance list | Start → |
| 03 | Search & Extend | Find elements and merge lists | index, count, extend, + |
Shopping cart merge | Start → |
| 04 | Organize Data | Sort, reverse, copy, delete | sort, reverse, copy, del, enumerate() |
Leaderboard ranking | Start → |
| 05 | Nested Lists | Rows, columns, matrix traversal | indexing on lst[i][j] |
Classroom seating chart | Start → |
Files 06–07 · Tuple (green)
| # | Lesson | What you learn | Key methods | Mini project | Open |
|---|---|---|---|---|---|
| 06 | Tuple Deep Dive | Immutability, packing, unpacking | tuple literals, * unpacking |
GPS coordinates | Start → |
| 07 | Tuple Methods | Only two tuple methods + sorted() |
index, count, sorted() |
Interview: why only two methods? | Start → |
Files 08–10 · Set (orange)
| # | Lesson | What you learn | Key methods | Mini project | Open |
|---|---|---|---|---|---|
| 08 | Set Basics | Uniqueness, creation, no indexing | add, remove, membership |
Unique visitors | Start → |
| 09 | Mutating Methods | Safe remove and in-place updates | discard, update, difference_update, pop |
Student club management | Start → |
| 10 | Set Algebra | Union, intersection, difference, Δ | union, intersection, difference, symmetric_difference, issubset |
Cricket vs football fans | Start → |
File 11 · Frozenset (purple)
| # | Lesson | What you learn | Key methods | Mini project | Open |
|---|---|---|---|---|---|
| 11 | Immutable Set | Read-only sets and hashability | copy, union, intersection, issubset |
Permission cache | Start → |
Files 12–13 · Dictionary (teal)
| # | Lesson | What you learn | Key methods | Mini project | Open |
|---|---|---|---|---|---|
| 12 | Dictionary Methods | Safe lookup and views | get, keys, values, items, update, pop |
Student profile | Start → |
| 13 | Nested Dictionaries | JSON-like records and CRUD | nested dict, update by key path |
Employee database | Start → |
Files 14–15 · Advanced (grey)
| # | Lesson | What you learn | Key methods | Mini project | Open |
|---|---|---|---|---|---|
| 14 | Hashability & Mixed Collections | What can be a dict key; nesting rules | hashable vs unhashable, conversions | Interview-heavy topic | Start → |
| 15 | Practice Programs | 25–30 problems across all types | all collection methods combined | Final revision module | Start → |
Difficulty progression
Production filenames (aligned)
| File | MahiDev path | URL slug |
|---|---|---|
| 02 | 02-list-methods-add-remove.md |
list-methods-add-remove |
| 03 | 03-list-methods-search-extend.md |
list-methods-search-extend |
| 04 | 04-list-sort-reverse-copy-del.md |
list-sort-reverse-copy-del |
| 05 | 05-nested-lists-and-matrices.md |
nested-lists-and-matrices |
| 06 | 06-tuple-deep-dive.md |
tuple-deep-dive |
| 07 | 07-tuple-methods-and-sorted.md |
tuple-methods-and-sorted |
| 08 | 08-set-creation-and-basics.md |
set-creation-and-basics |
| 09 | 09-set-methods-mutating.md |
set-methods-mutating |
| 10 | 10-set-algebra-and-relationships.md |
set-algebra-and-relationships |
| 11 | 11-frozenset-methods.md |
frozenset-methods |
| 12 | 12-dictionary-methods.md |
dictionary-methods |
| 13 | 13-nested-dictionaries-and-combinations.md |
nested-dictionaries-and-combinations |
| 14 | 14-hashability-and-mixed-collections.md |
hashability-and-mixed-collections |
| 15 | 15-collections-practice-programs.md |
collections-practice-programs |
Start here after this overview: Lesson 2 — List Add & Remove
Pre-defined functions — master index
On a list, you can use indexing and slicing to access and update values directly. Beyond that, each collection type exposes pre-defined methods. Use this index to jump to the right lesson.
| Type | Method count | Pre-defined functions | Lesson |
|---|---|---|---|
list |
12 | append, insert, remove, pop(index), pop(), clear, index, count, extend, copy, reverse, sort |
Lessons 2–5 |
tuple |
2 | index, count |
Lessons 6–7 |
set |
16 | add, clear, remove, discard, pop, isdisjoint, issuperset, issubset, union, intersection, difference, difference_update, symmetric_difference, symmetric_difference_update, update, copy |
Lessons 8–10 |
frozenset |
8 | copy, union, intersection, difference, symmetric_difference, isdisjoint, issubset, issuperset |
Lesson 11 |
dict |
9 | get, keys, values, items, update, pop, popitem, clear, copy |
Lessons 12–13 |
Also covered with lists:
deloperator (delete item, slice, or name) and built-inenumerate()— see table below and List sort, reverse, copy, del.
List — 12 pre-defined functions (full index)
On a list object you can use indexing and slicing to access and update values. Along with those operations, the list exposes 12 pre-defined functions in your notes:
#4 pop(index)and#5 pop()are separate entries (same method, two call forms).- Your handout labels both
reverse()andsort()as 11 — heresort()is correctly numbered 12.
| # | Method | Syntax | Purpose | On error |
|---|---|---|---|---|
| 1 | append() |
lst.append(value) |
Add one value at the end | — |
| 2 | insert() |
lst.insert(index, value) |
Insert at index (+ve or −ve); invalid +ve → end; invalid −ve → start | — |
| 3 | remove() |
lst.remove(value) |
Remove first matching value (by value) | ValueError if missing |
| 4 | pop(index) |
lst.pop(index) |
Remove and return item at index (+ve or −ve) | IndexError if bad index or empty |
| 5 | pop() |
lst.pop() |
Remove and return last element | IndexError on empty list |
| 6 | clear() |
lst.clear() |
Remove all elements; same object | Returns None |
| 7 | index() |
lst.index(value) |
Index of first occurrence | ValueError if missing |
| 8 | count() |
lst.count(value) |
Count occurrences | Returns 0 if absent |
| 9 | extend() |
lst1.extend(lst2) |
Merge all items from lst2 into lst1 in place |
TypeError if more than one arg |
| 10 | copy() |
lst2 = lst1.copy() |
Shallow copy — new list object | — |
| 11 | reverse() |
lst.reverse() |
Reverse in place; returns None |
— |
| 12 | sort() |
lst.sort() / lst.sort(reverse=True) |
Sort homogeneous data in place | TypeError on mixed types |
Between #6 and #7 in your notes: the
deloperator —del lst[i],del lst[a:b:c], ordel lst. Not a list method, but part of the same topic block.
Quick examples (from your notes)
lst = [10, "RS"]
lst.append("PYTHON")
lst.append(23.45)
print(lst) # [10, 'RS', 'PYTHON', 23.45] — same id()
lst = [10, 20, 30, 40, 50]
lst.insert(2, "RS")
lst.insert(-1, 155)
print(lst) # [10, 20, 'RS', 30, 40, 155, 50]
lst = [10, 20, 10, 30, 10, 40, 50]
lst.remove(10) # first 10 only
print(lst.pop(4)) # 10
print(lst.pop()) # 50
lst = [10, 20, 30, 10, 30, 20, 40, 10, 20]
print(lst.index(10), lst.count(10)) # 0, 3
lst1 = [10, 20, 30, 40]
lst1.extend(["RS", "TR", "DR"])
# Or: lst1 = lst1 + lst2 + lst3 # new list
lst1 = [10, "RS"]
lst2 = lst1.copy() # shallow — different id
lst1.append("Python")
lst = [10, 2, 13, -12, 56, 3, 7, 12]
lst.sort()
lst.reverse()Also know: del, enumerate(), and +
| Tool | Syntax | Purpose |
|---|---|---|
| Indexing / slicing | lst[i], lst[a:b] |
Read or update values directly |
del item |
del lst[i] |
Delete one element by index |
del slice |
del lst[a:b:c] |
Delete multiple elements |
del name |
del lst |
Remove variable name (GC collects object) |
enumerate() |
for i, v in enumerate(lst): |
All index–value pairs (find every match) |
+ merge |
lst1 + lst2 + lst3 |
New list from multiple lists |
Memory note:
lst2 = lst1is an alias (same object), not a copy. Uselst1.copy()for an independent list. See Aliasing and Copying.
Deep dives: Add & remove · Search & extend · Sort, copy, del · Nested lists
Set — 16 pre-defined functions (full index)
On a set, you cannot use indexing or slicing. All work goes through 16 pre-defined functions in your notes:
| # | Method | Purpose |
|---|---|---|
| 1 | add() |
Add one value |
| 2 | clear() |
Remove all elements |
| 3 | remove() |
Remove value — KeyError if missing |
| 4 | discard() |
Remove value — silent if missing |
| 5 | pop() |
Remove and return one arbitrary element |
| 6 | isdisjoint() |
True if no common elements |
| 7 | issuperset() |
True if self contains all of other |
| 8 | issubset() |
True if all of self are in other |
| 9 | union() |
All unique elements from both sets |
| 10 | intersection() |
Common elements only |
| 11 | difference() |
In self, not in other → new set |
| 12 | difference_update() |
Same as difference, mutates self |
| 13 | symmetric_difference() |
Elements in one set, not both (Δ) |
| 14 | symmetric_difference_update() |
Symmetric diff in place |
| 15 | update() |
Merge other set into self (union in place) |
| 16 | copy() |
Shallow copy — new set object |
After #5 in your notes:
del sremoves the variable name —del s[i]is not allowed on sets (TypeError).
Deep dives: Set basics · Mutating methods · Set algebra
Knowledge check
Q1: Which types support obj[0]?
Answer: Sequence types — list, tuple, str, etc. Not set or dict (dict uses keys).
Q2: Which type removes duplicates automatically?
Answer: set (and frozenset) — duplicate values collapse on creation.
Q3: How many pre-defined functions are in your list notes?
Answer: 12 — append, insert, remove, pop(index), pop(), clear, index, count, extend, copy, reverse, sort. (del and enumerate() are related but separate.)
Q4: How many pre-defined functions are in your set notes?
Answer: 16 — from add() through copy(), including difference_update and symmetric_difference_update.
Loading playground…