Reputation: 173
How can I create such board using list comperhension?
board = [[(0,0), (0,1), (0,2), (0,3), (0,4)],
[(1,0), (1,1), (1,2), (1,3), (1,4)],
[(2,0), (2,1), (2,2), (2,3), (2,4)],
[(3,0), (3,1), (3,2), (3,3), (3,4)],
[(4,0), (4,1), (4,2), (4,3), (4,4)]]
Upvotes: 1
Views: 174
Reputation: 20490
You can use a nested list-comprehension to achieve this as follows.
Run the for loop for the 2nd index in a inner list comprehension, and the for loop for the 1st index in the outer list comprehension
board = [ [(j,i) for i in range(5)] for j in range(5)]
print(board)
The output will be
[[(0, 0), (0, 1), (0, 2), (0, 3), (0, 4)],
[(1, 0), (1, 1), (1, 2), (1, 3), (1, 4)],
[(2, 0), (2, 1), (2, 2), (2, 3), (2, 4)],
[(3, 0), (3, 1), (3, 2), (3, 3), (3, 4)],
[(4, 0), (4, 1), (4, 2), (4, 3), (4, 4)]]
Upvotes: 1