python nested loop for a 2d list

Solutions on MaxInterview for python nested loop for a 2d list by the best coders in the world

showing results for - "python nested loop for a 2d list"
Silas
28 Sep 2019
1list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
2
3for i in range(0, len(list)): # loops over the external list
4    for j in range(0, len(list[i])): # loops over every internal list
5        print(list[i][j], end="\t") # prints the elements of the inner list seperated by a tab character
6    print("\n") # after every 3 iterations, print a new line/ shift to a new line
7    
8# OR you could use a much shorter way of writing the above nested loops
9# either one you choose, the logic is the same for both
10
11for i in list:
12    for j in i:
13        print(j, end="\t")
14    print("\n")
15
16# Expected Output:
17"""
181       2       3
19
204       5       6
21
227       8       9
23"""