printing a list of lists | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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()

29th Apr 2021, 9:34 PM
Alaa Aldeen Shammr
Alaa Aldeen Shammr - avatar
7 Answers
+ 3
[print(*row) for row in zip(*grid)]
29th Apr 2021, 10:20 PM
ChaoticDawg
ChaoticDawg - avatar
+ 2
ChaoticDawg Yes Thank you šŸ™
29th Apr 2021, 10:28 PM
Alaa Aldeen Shammr
Alaa Aldeen Shammr - avatar
+ 1
for i in range(len(grid[0])): for x in range(len(grid)): print(grid[x][i], end="") print()
29th Apr 2021, 9:49 PM
TOLUENE
TOLUENE - avatar
+ 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šŸ˜
30th Apr 2021, 4:44 AM
Alaa Aldeen Shammr
Alaa Aldeen Shammr - avatar
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]
29th Apr 2021, 9:56 PM
Alaa Aldeen Shammr
Alaa Aldeen Shammr - avatar
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].....
29th Apr 2021, 10:03 PM
TOLUENE
TOLUENE - avatar
0
Jan Markus This also works šŸ’Ŗ
30th Apr 2021, 4:29 AM
Alaa Aldeen Shammr
Alaa Aldeen Shammr - avatar