Excellent choice.
LIST MASTERY (Module 2) is where coding interviews are won or lost. Lists look simple, but most bugs, slowdowns, and tricky questions live here.
We’ll do this the same way as strings: internals → methods → traps → interview patterns.
🔥 MODULE 2 — LIST MASTERY
Day 1: Internals, Core Methods & Dangerous Traps




🧠 MODULE GOAL (LISTS)
By the end of List Mastery, you will:
- Know every important list method
- Understand time complexity of each operation
- Avoid silent O(n²) bugs
- Solve list interview problems confidently
- Explain behavior using CPython internals
1️⃣ WHAT A PYTHON LIST REALLY IS (CRITICAL)
A Python list is:
- A dynamic array
- Stores references to PyObject
- Over-allocates memory to amortize growth
lst = [1, 2, 3]
Internally:
PyListObject
├── ob_size (logical length)
├── allocated (capacity)
└── ob_item → [ptr, ptr, ptr, ...]
🧠 List elements are references, not values
2️⃣ WHY append() IS FAST (AMORTIZED O(1))
When list grows:
- Python allocates extra space
- Avoids reallocating every append
Growth strategy (simplified):
0 → 4 → 8 → 16 → 25 → 35 → ...
So:
lst.append(x) # Amortized O(1)
But:
lst.insert(0, x) # O(n)
3️⃣ LIST METHODS — COMPLETE & GROUPED
We group by intent (this is how interviewers think).
➕ A. ADDING ELEMENTS (VERY IMPORTANT)
append(x)
lst.append(10)
- Adds ONE element
- O(1) amortized
🔥 extend(iterable)
lst.extend([1, 2, 3])
Equivalent to:
for x in iterable:
lst.append(x)
⚠️ Common trap:
lst.append([1,2]) # [[1,2]]
lst.extend([1,2]) # [1,2]
Interviewers LOVE this difference.
insert(index, x)
lst.insert(0, 99)
- Shifts elements
- O(n)
- Avoid in large lists
➖ B. REMOVING ELEMENTS
pop()
lst.pop() # last element (O(1))
lst.pop(0) # O(n)
remove(value)
lst.remove(3)
- Removes first occurrence
- Searches linearly → O(n)
clear()
lst.clear()
- Removes all references
- List object reused
🔍 C. INSPECTION
index(x)
lst.index(10)
- Returns first index
- Raises
ValueErrorif not found
count(x)
lst.count(5)
- Full scan → O(n)
🔄 D. REORDERING
sort()
lst.sort()
Facts interviewers expect:
- In-place
- Stable
- Timsort
- O(n log n) worst-case
- O(n) on nearly-sorted data
reverse()
lst.reverse()
- In-place
- O(n)
sorted(lst) vs lst.sort()
sorted() | sort() |
|---|---|
| Returns new list | In-place |
| Works on any iterable | Only list |
| Safer | Faster |
📋 E. COPYING (VERY IMPORTANT TRAPS)
Shallow copy methods
lst.copy()
lst[:]
list(lst)
All are shallow copies.
🔥 Shallow Copy Trap
a = [[1,2],[3,4]]
b = a.copy()
b[0].append(99)
print(a) # [[1,2,99],[3,4]]
Why?
- Inner lists shared
- Only outer list copied
4️⃣ THE MOST DANGEROUS LIST TRAPS 🚨
❌ Trap 1: list * n
lst = [[0]] * 3
lst[0].append(1)
print(lst)
Output:
[[0,1],[0,1],[0,1]]
Why?
- Same inner list reference repeated
Correct:
lst = [[0] for _ in range(3)]
❌ Trap 2: Removing while iterating
for x in lst:
if x == 0:
lst.remove(x)
Bug:
- Index shifts
- Elements skipped
Correct:
lst[:] = [x for x in lst if x != 0]
❌ Trap 3: Membership test in list
if x in lst: # O(n)
Use set when:
- Frequent membership checks
5️⃣ LIST SLICING — COST MODEL
lst[a:b]
- Creates new list
- O(k) where k = slice size
Bad in loops:
for i in range(n):
lst[i:i+10] # ❌ expensive
6️⃣ LIST COMPREHENSIONS — FAST PATH
[x*x for x in data if x > 0]
Why faster than loop:
- Loop in C
- Fewer bytecode ops
But:
- Don’t over-nest
- Readability > cleverness
🔥 LIST INTERVIEW QUESTIONS (DAY 1 SET)
Q1. Difference between append and extend?
(Expected explanation + example)
Q2. Why is pop() fast but pop(0) slow?
Q3. Why list * n is dangerous?
Q4. When to use sorted() vs sort()?
Q5. How to copy a list safely with nested elements?
(Expected: copy vs deepcopy)
🧠 LIST MENTAL CHECKLIST
Before writing list code:
- Append or extend?
- Front or back operation?
- Copy depth?
- Membership frequency?
- Slice cost hidden?
📝 MODULE 2 — DAY 1 ASSIGNMENT
1️⃣ Predict output (NO RUNNING)
lst = [[1]] * 2
lst[0].append(2)
print(lst)
2️⃣ Choose correct method (Explain WHY)
lst = []
Add elements from another iterable efficiently.
3️⃣ Coding
Write a function:
def remove_duplicates_preserve_order(lst):
🔜 NEXT — LIST MASTERY DAY 2 🔥
DAY 2
- Two-pointer & sliding window on lists
- In-place interview problems
- Rotation, reversal, merging
- 20+ list coding questions
👉 Say “CONTINUE LIST MASTERY – DAY 2” when ready.