Python program to iterate over a 2D list in different ways

Write a python program to iterate over a 2D list in different ways.

Method-01

In this method we are using the length of list to iterate 2D list.

list2d = [["A","B","C","Demo"],[2.1,2.2,2.3],[4,5,6,7]]
for i in range(len(list2d)):
    for j in  range(len(list2d)):
        print(list2d[i][j])

Output:

A
B
C
2.1
2.2
2.3
4
5
6

 

Method-02

In this method we are using list object directly to iterate the 2D list. First iteration select index of list and second iteration extract all values of that index.

list01 = [["A","B","C","Demo"],[2.1,2.2,2.3],[4,5,6,7]]

for i in list01:
    for j in i:
        print(j)

Output:

A
B
C
2.1
2.2
2.3
4
5
6

 

Leave a comment