+ 2
printing a list of lists
i have this list of lists grid = [['.', '.', '.', '.', '.', '.'], ['.', 'O', 'O', '.', '.', '.'], ['O', 'O', 'O', 'O', '.', '.'], ['O', 'O', 'O', 'O', 'O', '.'], ['.', 'O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O', '.'], ['O', 'O', 'O', 'O', '.', '.'], ['.', 'O', 'O', '.', '.', '.'], ['.', '.', '.', '.', '.', '.']] and i want to print it like this : ..OO.OO.. .OOOOOOO. .OOOOOOO. ..OOOOO.. ...OOO... ....O.... i need to use a loop in a loop in order to print grid[0][0], then grid[1][0], then grid[2][0], and so on, up to grid[8][0] my attemps: for i in range(len(grid)): for x in range(len(grid[i])): print(grid[i][x], end="") print()
7 Respostas
+ 3
[print(*row) for row in zip(*grid)]
+ 2
ChaoticDawg
Yes
Thank you 🙏
+ 1
for i in range(len(grid[0])):
for x in range(len(grid)):
print(grid[x][i], end="")
print()
+ 1
In fact, i faced this question while I'm studying a book,
And it asked for another way to solve this,
(Hint: You will need to use a loop in a loop in order to print grid[0][0], then grid[1][0], then grid[2][0], and so on, up to grid[8][0]. This will finish the first row, so then print a newline. Then your program should print grid[0][1], then grid[1][1], then grid[2][1], and so on. The last thing your program will print is grid[8][5].
Also, remember to pass the end keyword argument to print() if you don’t want a newline printed automatically after each print() call.)
You also provided advance ways,and i really appreciate it.
After all, all ways lead to rome😁
0
i want to to print grid[0][0],
then grid[1][0], then grid[2][0], and so on, up to grid[8][0]
0
Ala'a Aldeen Shammr mine answer code also do the same.
for i in (0,5)
for j in(0,8)
[j][i]
This goes through like this
[0][0],[1][0],[2][0],....
[0][1],[1][1],[2][1].....
.................................
[0][5],[1][5],[2][5].....
0
Jan Markus
This also works 💪