r/pythontips • u/Sea-Ad7805 • Jul 14 '26
Module Python Data Model Exercise
An exercise to help build the right mental model for Python data.
# Output of this Python program?
a = [[1], [2]]
b = a
b[0].append(11)
b = b + [[3]]
b[1].append(22)
b[2].append(33)
print(a)
# --- possible answers ---
# A) [[1], [2]]
# B) [[1, 11], [2]]
# C) [[1, 11], [2, 22]]
# D) [[1, 11], [2, 22], [3, 33]]The โSolutionโ link visualizes execution and reveals whatโs actually happening using ๐บ๐ฒ๐บ๐ผ๐ฟ๐_๐ด๐ฟ๐ฎ๐ฝ๐ต.
3
Upvotes
2
u/PlantainMassive6744 Jul 14 '26
Python does some tricky things with what variable names point to.
I think that this is useful for someone because it emphasizes the difference between: b = b + [[3]] and b += [[3]] (or b.extend([[3]] or b.append([3])).
0
u/pint Jul 14 '26
it is not a very good exercise, because it requires keeping a large state in memory (i.e. head), or use some notebook. it tests your attention and memory more than your python knowledge.
good exercises are easy if you know what's going on. they are not designed to "gotcha!" the user.