List & Tuple
Building Lists of Lists
Method 1
board = [['_'] * 3 for i in range(3)]
print(board)
>>> [['_', '_', '_'], ['_', '_', '_'], ['_', '_', '_']]
board[1][2] = 'X'
print(board)
>>> [['_', '_', '_'], ['_', '_', 'X'], ['_', '_', '_']]board = []
for i in range(3):
# Each iteration builds a new row and
# appends it to board
row = ['_'] * 3
# Only row 2 is changed, as expected
board.append(row)Method 2
Augmented Assignment with Sequences
Playground
Last updated
Was this helpful?