In the Tetris game in the PyGame book, they create a board using:
board = []
for i in range(BOARDWIDTH):
board.append([BLANK] * BOARDHEIGHT)
return board
While that's fine, a list of lists, I was thinking about how they usually represent that in Java and other languages, as an array with set index - for example, int board[][] = new int[8][8]; // create 64 integers
(a 2d array example).
From what I understand, tuples are more similar to the array (vs List which is more like ArrayList or LinkedList of Java). They have set number of elements and can't be added to, and hold a set number of items.
Why would we not create a tuple of tuples for a board, for example
board = ( (BLANK,)*10 ,)*10
Do they use lists of lists for some optimization in Python's internal representation of the board, or just because they assume users know List more commonly than Tuple?
This has the added benefit of being functional and making immutable objects. If you want to make a board with a state in which you moved in tic tac toe for example, you could try something like:
tictac = ((0,0,0),)*3
vals = (tuple((tictac[r][c]) if r!=1 or c!=1 else 'x' for (r,row) in enumerate(tictac)) for c in range(3))
for i in vals: print i
to move a 1 to the center.
List comprehensions are quite flexible in Python and this is more functionally-correct.