Interview Questions: Lists & Tuples
Lists and tuples are where interviewers start probing whether you actually understand what a variable "holding" a value means. This set goes after the classic traps — aliasing, shallow copies, mutability by reference — plus the practical questions about which tool to reach for and why.
1 When would you use a list instead of a tuple, and vice versa?
Reach for a list when the collection needs to change shape over its lifetime — items get added, removed, reordered, or updated (a shopping cart, a queue of tasks, scores collected as they come in). Reach for a tuple when the collection represents a fixed-size, fixed-meaning record that shouldn't change once created — a coordinate pair (x, y), an RGB color (255, 0, 0), a database row. Tuples also signal intent to anyone reading the code: "this is a small fixed bundle, not a growing collection."
Treating the choice as purely stylistic, or answering "tuples are just faster" without being able to explain when that speed difference would actually matter.
Follow-up"Would you use a list or a tuple to represent a single point on a 2D grid?" (A tuple — (x, y) is a fixed two-item record that shouldn't be resized.)
2 What does it actually mean for a list to be "mutable" in practice?
A list's contents can be changed in place without creating a new object — my_list[0] = "new" overwrites the first slot directly, and methods like .append(), .sort(), and .remove() modify the same object rather than returning a new one. This is the opposite of a string, which must always build a brand-new object for any "change." The practical consequence is that anyone else holding a reference to that same list sees the change too — mutation isn't private to the variable name you used.
Assuming my_list.append(x) returns a new list you need to capture with = — it returns None and mutates the original in place.
"What does x = my_list.append(5) store in x?" (None — .append() mutates my_list in place and doesn't return anything useful.)
3 If b = a and then you modify b, why does a change too?
Because b = a doesn't copy the list — it makes b a second name pointing at the exact same list object in memory. There's only ever one list here, with two labels on it. So b.append(3) mutates that one shared object, and looking at it through either name (a or b) shows the same updated contents. This is the single most common real-world list bug: passing a list around and accidentally mutating a copy someone else still expects to be untouched.
Believing b = a creates an independent copy of a list the way it might feel like it does for a number or a string.
"How would you check whether a and b are the same object versus just equal in value?" (a is b checks identity/same-object; a == b checks value equality — two independent lists with the same contents are == but not is.)
4 How do you actually make an independent copy of a list?
Any of b = a.copy(), b = a[:] (a full slice), or b = list(a) creates a new, separate list object holding the same elements — from then on, appending to b does not affect a. All three do the same job at this level: a shallow copy, meaning the new outer list is independent, but if any element inside it is itself a mutable object (like a nested list), that inner object is still shared between the two copies.
Writing b = a and calling it "making a copy" — that's aliasing, not copying.
"After a = [1, 2], b = a.copy(), b.append(3) — what is a?" (Still [1, 2] — the copy was independent, so appending to b never touched a.)
5 What's the gotcha with a shallow copy when the list contains nested lists?
A shallow copy only copies the outer list — each inner list is still the same shared object in both copies. So if grid = [[0, 0, 0]] * 3 is built by repeating one inner list three times, all three "rows" are actually the same list in memory, not three independent ones. Mutating grid[0][0] = 1 silently changes grid[1][0] and grid[2][0] too, because there's really only one row object being pointed at three times. The fix at this course's level is building each row separately in a loop (e.g. grid.append([0, 0, 0]) three times) instead of multiplying one shared row.
Using [[0] * n] * m to build a 2D grid and being surprised when editing one "row" changes every row.
"After grid = [[0, 0, 0]] * 3 and grid[0][0] = 1, what does grid look like?" ([[1, 0, 0], [1, 0, 0], [1, 0, 0]] — every row shows the change, since all three are the same underlying list object.)
6 Why is .append() considered cheap, but .insert(0, x) considered expensive on a long list?
.append(x) just drops x onto the end — nothing else in the list has to move. .insert(0, x) has to shift every single existing element one slot to the right to make room at the front, before x can go in. On a list of length n, that's roughly n elements moved for one insert at the front, versus a constant amount of work for one append at the end. Repeatedly inserting at the front of a large list in a loop is a common performance smell; repeatedly appending is not.
Treating .append() and .insert(0, x) as equally cheap operations regardless of list size.
"If you needed to repeatedly add items to the front of a large collection, is a plain list your best structural choice?" (Not ideally — every front-insert re-shifts the whole list; this is a case where a different structure, covered later in the course, would do better. For now, appending and reversing at the end, or restructuring the problem so you append instead, avoids the repeated shifting.)
7 What's the difference between .sort() and sorted()?
my_list.sort() sorts the list in place and returns None — the original list itself is now rearranged. sorted(my_list) leaves the original list untouched and returns a brand-new sorted list, which you have to capture in a variable to use. A very common bug is writing my_list = my_list.sort(), expecting a sorted list back, and instead wiping out the variable with None.
Writing x = some_list.sort() and being confused why x is None.
"If you need the original list's order preserved but also want a sorted version, which do you use?" (sorted(my_list) — it returns a new list without touching the original.)
8 Does slicing a list (e.g. my_list[1:3]) modify the original list?
No — slicing always builds and returns a new list containing copies of the references to the selected elements; the original list is completely untouched. This is true even for a full slice like my_list[:], which is actually a common, deliberate way to make a shallow copy of an entire list. Slicing never mutates in place the way .sort() or .append() do.
Assuming a slice is a "view" into the same list that shares mutations both ways — in Python, a list slice is always an independent new list.
Follow-up"After a = [1, 2, 3, 4], b = a[1:3], b.append(99) — what is a?" (Still [1, 2, 3, 4] — b is an independent new list, so mutating it never touches a.)
9 What's a practical use case for tuple packing and unpacking?
Packing is bundling several values into one tuple, like point = (3, 4). Unpacking is pulling a tuple's values back out into separate variables in one line: x, y = point. The classic everyday use is swapping two variables without a temp variable — a, b = b, a works because Python first packs the right side into a temporary tuple (b, a), then unpacks it into a and b. It's also used constantly when looping over a list of paired records, e.g. for name, score in student_scores:, unpacking each pair on every iteration.
Not recognizing a, b = b, a as tuple packing/unpacking under the hood, or unpacking into the wrong number of variables and not knowing that raises a ValueError.
"What happens if you try x, y = (1, 2, 3)?" (ValueError — there are 3 values but only 2 variables to unpack into; the counts must match exactly.)
10 What's the difference between == and is when comparing two lists?
== compares values element by element — two separate list objects with identical contents in the same order are ==. is compares identity — whether both names point at the literal same object in memory. Unlike small integers or interned strings, two separately built lists with equal contents are essentially never is the same object, even though they're ==. This is the same distinction that causes the b = a aliasing trap in question 3, just from the comparison side.
Using is to check whether two lists have the same contents — that's what == is for.
"Are [1, 2] and [1, 2], written as two separate literals, ever the same object?" (Practically no — each list literal creates a brand-new list object, so [1, 2] is [1, 2] is False even though [1, 2] == [1, 2] is True.)
11 What's the difference between .remove() and .pop()?
.remove(value) searches for the first element that equals value and deletes it — you specify what the element looks like, not where it is. It raises ValueError if no matching value exists. .pop(index) deletes and returns the element at a specific position — you specify where, not what's there — and defaults to removing the last element if no index is given. It raises IndexError for an out-of-range index. The two solve different problems: use .remove() when you know the value but not its position; use .pop() when you know the position (or just want "the last one") and need the removed value back.
Calling .remove(2) expecting it to remove the element at index 2, rather than the first element equal to 2.
"Given nums = [5, 10, 15], what does nums.remove(2) do?" (Raises ValueError — there is no element equal to 2 in the list, even though index 2 exists.)
12 What's the difference between .append() and .extend()?
.append(x) adds exactly one new element to the end of the list — even if x is itself a list, it gets added whole, as one single nested element. .extend(iterable) adds each element of the given iterable individually, growing the list by however many items the iterable contains. So [1, 2].append([3, 4]) gives [1, 2, [3, 4]] (three elements, one of which is a nested list), while [1, 2].extend([3, 4]) gives [1, 2, 3, 4] (four flat elements).
Using .append() when trying to merge two lists together and ending up with an accidentally nested list.
"What does [1, 2].append([3, 4]) produce, versus [1, 2].extend([3, 4])?" ([1, 2, [3, 4]] versus [1, 2, 3, 4].)
13 Why can a tuple be used as a dictionary key, but a list can't?
Dictionary keys (and set elements, coming in Chapter 8) must be hashable — Python needs to compute a stable hash value for a key that never changes for the object's lifetime, so it can look the key up quickly. Because tuples are immutable, their contents (and therefore their hash) can never change after creation, so they're hashable and valid as keys — as long as everything inside the tuple is also hashable. Lists are mutable, so their contents (and hash) could change at any time; Python refuses to hash them at all, and hash([1, 2]) raises TypeError: unhashable type: 'list'.
Trying to use a list as a dictionary key and expecting it to work like a tuple would.
Follow-up"Would (1, [2, 3]) — a tuple containing a list — be hashable?" (No — a tuple is only hashable if every element inside it is also hashable; a mutable list inside it makes the whole tuple unhashable too.)
14 What's a common gotcha when checking membership or looping over a list of records?
The in operator on a list has to scan element by element from the start until it finds a match (or reaches the end) — there's no shortcut, so checking membership repeatedly inside a loop over a large list can quietly turn an O(n) task into an O(n²) one, the same shape of problem as repeated string concatenation from Chapter 6. A second common gotcha is looping over a list of paired records (like [name, score] pairs) with a plain for item in records: and then having to manually index item[0]/item[1] — unpacking directly in the loop header, for name, score in records:, is clearer and less error-prone.
Running an in check on a large list inside a loop without recognizing the repeated linear-scan cost, or manually indexing item[0]/item[1] when direct unpacking in the loop header is available and clearer.
"Given records = [["Ana", 91], ["Ben", 78]], how would you loop over it and print each name and score cleanly?" (for name, score in records: print(name, score) — unpacking each inner pair directly in the loop header.)
Rapid-Fire Review
Quick recall check — fill in each blank and hit Check.