Home βΊ Problems βΊ List multiplication
List multiplication
π§ Predict the output Topic: lists Difficulty: medium Free
What this problem practises
Lists are mutable and assigned by reference. That one fact explains most list bugs: aliasing between what look like "copies", append() and sort() returning None, and slice boundaries that disagree with the index arithmetic.
This is an output-prediction problem: read the snippet carefully and write down exactly what it prints before anything runs. These questions are interview staples because they test whether you know the language rules, or merely recognise their syntax.
Difficulty: medium β expect to hold two interacting rules in your head at once, such as a boundary combined with a mutation.
The challenge
The code runs in PyDebugβs in-browser compiler. Open the problem, run the snippet, watch the exact error or output, and keep iterating until it is right. Signed-in solves count toward XP and your daily streak.
The snippet
The interactive editor pre-fills exactly this code β read it, then submit your prediction:
row = [0] * 3
print(row)
grid = [row] * 2
grid[0][0] = 9
print(grid)Understand the rule
[0] * 3 gives [0, 0, 0] β handy. [[0]] * 3 gives three references to the same inner list β a trap. List multiplication copies references, not objects, so mutating one row of a multiplied nested list changes every row. Predicting this output requires distinguishing flat multiplication (safe, values are copied by reference but numbers are immutable) from nested multiplication (shared mutable rows).
How to trace it
- Expand the multiplication: how many elements does the outer list now have?
- Ask whether the repeated element is mutable: numbers and strings are safe; inner lists and dicts are shared.
- On any mutation like grid[0][0] = 1, apply it to the one shared inner object β then read every row through that shared object.
- Print with this sharing in mind: all rows show the change.
Common wrong fixes
- Expecting [[0] * 3] * 2 to produce independent rows; both rows are the same list object.
- Fixing it by copying after the fact instead of building independently: [[0] * 3 for _ in range(2)].
- Confusing this with slicing copies β a slice makes a new outer list but still shares the inner ones (shallow copy).
Rule to remember
List multiplication repeats references. For nested structures, use a comprehension to create fresh inner lists β and when output looks 'impossibly' repeated, suspect shared objects.
π¦ Learning path: Lists and Dictionaries
Problem 7 of 20 in this path. Work them in order β each one builds on the rule before it.
πΌ Learning path: Interview Practice
Problem 4 of 16 in this path. Work them in order β each one builds on the rule before it.
β List copy vs referenceNext in path: Loop variable after the loop ends β
More lists problems
- Looping one step too far easy
- List slicing with step medium
- in operator on a list easy
Keep going
- Output-prediction strategies β how to predict tricky snippets without running them.
- Browse every lists problem in this topic.
- Common Python errors and what they actually mean, plus the Python glossary for any term used on this page.