OCJP

Multidimension (Two Dimension) 2D Array in Python

We can use Multi Dimension Array in Python. Following example shows the syntax and use of Multidimension Array

mat=[[3,4,4],[2,3,5],[2,4,5]]

for row in mat:
    for ele in row:
        print(ele,end=' ') #Next print is same line
    print() # for new line after row


marks=[['Student 1',45,90],['Student 2',48,53],['Student 3',59,35]]
print(marks)

for record in marks:
    total=0
    for e in record:
        print(e,end=' ')
        if(type(e)==int):
            total+=e
    record.append(total)
    print()

print(marks)

Output for the same will be like:

3 4 4 
2 3 5 
2 4 5 
[['Student 1', 45, 90], ['Student 2', 48, 53], ['Student 3', 59, 35]]
Student 1 45 90 
Student 2 48 53 
Student 3 59 35 
[['Student 1', 45, 90, 135], ['Student 2', 48, 53, 101], ['Student 3', 59, 35, 94]]

Leave a Reply

Your email address will not be published. Required fields are marked *


× How can I help you?