Chapter 05

Lists & Slicing

Shallow copy traps, list multiplication gotchas, slicing edge cases, and the difference between sort() and sorted(). Lists hide many surprises.

Questions: 18
Format: Predict Output + Multiple Choice
Difficulty: ★★★☆☆
Key theme: The difference between mutating an object and rebinding a variable is the core of most list gotchas. When two names point to the same list object, mutations affect both.
Shallow vs Deep Copy
Q 01Predict the Output
a = [1, 2, 3]
b = a.copy()   # shallow copy
b.append(4)
print(a)
print(b)
Output
[1, 2, 3] [1, 2, 3, 4]
Why? .copy() creates a new list, so a and b are separate objects. Appending to b doesn't affect a. Same result with b = a[:] or b = list(a).
Q 02Predict the Output
import copy
a = [[1, 2], [3, 4]]
b = a.copy()     # shallow copy of outer list
b[0].append(99)   # mutate inner list
print(a)
print(b)
Output
[[1, 2, 99], [3, 4]] [[1, 2, 99], [3, 4]]
Why? A shallow copy creates a new outer list but the inner lists are still shared references. b[0] and a[0] point to the same inner list [1, 2]. Mutating it affects both. To avoid this, use copy.deepcopy(a) which recursively copies all nested objects.
Q 03Multiple Choice
grid = [[0] * 3] * 3   # 3x3 grid of zeros
grid[0][0] = 1
print(grid)
Answer: b) [[1,0,0],[1,0,0],[1,0,0]].

[[0]*3]*3 creates three references to the same inner list. Modifying grid[0][0] modifies the shared inner list — all three rows change. To create a true 2D grid, use: [[0]*3 for _ in range(3)] — this creates 3 independent inner lists.
Slicing
Q 04Predict the Output
a = [0, 1, 2, 3, 4]
print(a[1:3])
print(a[-2:])
print(a[::2])
print(a[::-1])
print(a[1:10])  # beyond end
Output
[1, 2] [3, 4] [0, 2, 4] [4, 3, 2, 1, 0] [1, 2, 3, 4]
Why? Key slicing facts: [start:stop] — stop is exclusive. [-2:] — last 2 elements. [::2] — every 2nd element. [::-1] — reverse. [1:10] on a 5-element list — no error, just clips to the end. Slicing never throws IndexError.
Q 05Multiple Choice
a = [0, 1, 2, 3, 4]
a[1:3] = [10, 20, 30]
print(a)
Answer: b) [0, 10, 20, 30, 3, 4].

Slice assignment replaces the slice with the right-hand side iterable, even if lengths differ. a[1:3] (elements 1 and 2 — two items) is replaced with [10, 20, 30] (three items). The list grows to accommodate. This is a powerful but surprising feature.
Q 06Predict the Output
a = [1, 2, 3]
b = a[:]   # full slice = copy
print(a == b)
print(a is b)
Output
True False
Why? a[:] creates a new list with the same elements. == compares element-by-element → True. But is checks object identity — they are different list objects → False. a[:] is one of the most common idioms for copying a list.
sort() vs sorted()
Q 07Predict the Output
a = [3, 1, 4, 2]
b = sorted(a)
print(a)
print(b)
a.sort()
print(a)
Output
[3, 1, 4, 2] [1, 2, 3, 4] [1, 2, 3, 4]
Why? sorted(a) returns a new sorted list — a is unchanged. a.sort() sorts in place — a is modified — and returns None. A common bug: a = a.sort() sets a to None because .sort() returns None.
Q 08Multiple Choice
a = [3, 1, 2]
result = a.sort()
print(result)
print(a)
Answer: a) None, [1, 2, 3].

.sort() sorts in place and returns None. This is a classic trap: people write a = a.sort() expecting the sorted list, but a becomes None. Use sorted(a) when you need the result as a value.
List Methods
Q 09Predict the Output
a = [1, 2, 2, 3, 2]
a.remove(2)
print(a)
print(a.count(2))
Output
[1, 2, 3, 2] 2
Why? .remove(x) removes only the first occurrence of x. It does not remove all occurrences. After removing the first 2, the list still has two 2s. If the value doesn't exist, .remove() raises ValueError.
Q 10Predict the Output
a = [1, 2, 3]
print(a.pop())
print(a.pop(0))
print(a)
Output
3 1 [2]
Why? .pop() with no argument removes and returns the last element. .pop(0) removes and returns the element at index 0. After both pops, only [2] remains. Note: .pop(0) is O(n) — it shifts all elements. Use collections.deque for efficient front pops.
List Multiplication
Q 11Multiple Choice
a = [0] * 3
a[0] = 5
print(a)
Answer: a) [5, 0, 0].

Integers are immutable. [0]*3 creates 3 references to the same integer 0, but since integers are immutable, a[0] = 5 simply replaces the reference at index 0. The other elements still reference 0. This is safe for immutable types. The danger appears with mutable types like lists (see Q03).
Q 12Predict the Output
a = [1, 2] * 3
print(a)
print(len(a))
Output
[1, 2, 1, 2, 1, 2] 6
Why? List multiplication repeats the list contents. [1, 2] * 3 concatenates the list 3 times. The resulting list has 6 elements. Since the elements are integers (immutable), this is safe — no shared reference problems.
Indexing
Q 13Predict the Output
a = [10, 20, 30, 40]
print(a[-1])
print(a[-2])
print(a[-4])
print(a[100] if 100 < len(a) else "out")
Output
40 30 10 out
Why? Negative indices count from the end: -1 is last, -2 is second-to-last, -4 is first (for a 4-element list). Accessing index 100 directly would raise IndexError — the ternary guard prevents that.
Q 14Multiple Choice
a = [1, 2, 3]
print(a[5])
Answer: c) IndexError.

Direct index access raises IndexError when out of bounds. But slicing (a[1:100]) does NOT raise an error — it just clips. This asymmetry is important to know.
extend vs append
Q 15Predict the Output
a = [1, 2]
b = [1, 2]
a.append([3, 4])
b.extend([3, 4])
print(a)
print(b)
Output
[1, 2, [3, 4]] [1, 2, 3, 4]
Why? .append(x) adds x as a single element — so a list becomes a nested element. .extend(iterable) adds each element of the iterable individually — no nesting. A very common confusion.
Q 16Multiple Choice
a = [1, 2, 3]
b = [4, 5]
c = a + b
d = a
d += b
print(a is d)
print(c is a)
Answer: a) True, False.

c = a + b creates a new list — so c is a is False. d = a makes d an alias for a. Then d += b calls d.__iadd__(b) — in-place extension — so d (and a) get the new items. a is d remains True.
Q 17Predict the Output
a = [3, 1, 2]
b = ["banana", "apple", "cherry"]
print(sorted(a, reverse=True))
print(sorted(b, key=len))
Output
[3, 2, 1] ['apple', 'banana', 'cherry']
Why? sorted(a, reverse=True) sorts in descending order. sorted(b, key=len) sorts strings by their length: "apple" (5), "banana" (6), "cherry" (6). When lengths tie, Python preserves original order (stable sort) — so "banana" comes before "cherry".
Q 18Multiple Choice
a = [1, 2, 3]
print(a * 0)
print([] + [])
print(len([None] * 5))
Answer: a) [], [], 5.

Multiplying a list by 0 gives an empty list. Adding two empty lists gives an empty list. [None]*5 creates a list of 5 None values — very useful for pre-allocating fixed-size lists.
← Chapter 04: Functions & Scope Chapter 06: Strings →