Python Program to Transpose a Matrix



Hey Friends, Here is a Python program to transpose a matrix,




 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# Program to transpose a matrix
# using nested loop

x = [[12,7],[4,5],[3,8]]
result = [[0,0,0],[0,0,0]]

# iterate through rows

for i in range(len(x)):
    # iterate through columns
    for j in range(len(x[0])):
        result[j][i] = x[i][j]
for r in result:
    print(r)

Output:

[12, 4, 3]
[7, 5, 8]

Comments