Back to questions
JuniorTheoryCommon
What does the program print?
grid = [[0] * 2] * 2
grid[0][0] = 5
print(grid)[[5, 0], [5, 0]]
- Expecting
[[5, 0], [0, 0]], as if* 2built two rows: the outer* 2repeats a reference to one inner list, so both rows are the same list. - Expecting every cell to become
5:[0] * 2repeats the number0, and assigninggrid[0][0]replaces one cell of the row, not the other.
- How would you build a 2 × 2 grid whose rows are independent lists?
- What does
print(grid[0] is grid[1])print here?