How to print matrix in python using for loop?

5903

In this article, we will discuss How to print matrix in python using for loop. This below code demonstrates print of 2D or two-dimensional matrix list. For an nxn size matrix, you need to run for loop for n times. Sometimes, the column size can be of varying size. so you need to individually count the size of nested columns.

Python Code to print matrix using for loop

 

Python code implementation without user-defined functions & classes

Code: 
array_items = [[10, 12, 13, 14], [15, 16], [19, 110, 111], [113, 114, 115, 116]]
count = 0
for i in range(0, len(array_items)):
    temp = len(array_items[i])

    for j in range(0, temp):
        count = count + 1
        print(count, " Item of array is: ", array_items[i][j])

 

Output:
Print the elements of array using For loop
1  Item of array is:  10
2  Item of array is:  12
3  Item of array is:  13
4  Item of array is:  14
5  Item of array is:  15
6  Item of array is:  16
7  Item of array is:  19
8  Item of array is:  110
9  Item of array is:  111
10  Item of array is:  113
11  Item of array is:  114
12  Item of array is:  115
13  Item of array is:  116