The built-in list
constructor is a common way of forcing iterators and generators to fully iterate in Python. When you call map, it only returns a map object instead of actually evaluating the mapping, which is not desired by the author of your code snippet.
However, using map
just to print all the items of an iterable on separate lines is inelegant when you consider all the power that the print function itself holds in Python 3:
>>> board = [[0]*3]*3
>>> board[0] is board[1]
True
>>> "Uh oh, we don't want that!"
"Uh oh, we don't want that!"
>>> board = [[0]*3 for _ in range(3)]
>>> board[0] is board[1]
False
>>> "That's a lot better!"
"That's a lot better!"
>>> print(*board, sep='\n')
[0, 0, 0]
[0, 0, 0]
[0, 0, 0]
Additional Note: In Python 2, where print
is treated as a statement, and is not so powerful, you still have at least two better options than using map
:
- Use a good old for-loop:
for row in board: print row
- Import Python 3's print function from the
__future__
module:
from __future__ import print_function