# Matrix transpose without NumPy in Python

In this article, we will understand how to do transpose a matrix without NumPy in Python. So, first, we will understand how to transpose a matrix and then try to do it not using NumPy.

## What is the Transpose of a Matrix?

Matrix is the representation of an array size in rectangular filled with symbols, expressions, alphabets and numbers arranged in rows and columns.

Now, we have to know what is the transpose of a matrix?

The matrix whose row will become the column of the new matrix and column will be the row of the new matrix. Therefore, we can implement this with the help of Numpy as it has a method called transpose().

Let’s implement with the help of NumPy:

### PROGRAM CODE:

import numpy as np arr1 = np.array([[1, 2, 3], [4, 5, 6]]) print(f'Original Array:\n{arr1}') arr1_transpose = arr1.transpose() print(f'Transposed Array:\n{arr1_transpose}')

**OUTPUT:**

Original Array: [[1 2 3] [4 5 6]] Transposed Array: [[1 4] [2 5] [3 6]]

But, we have already mentioned that we cannot use the Numpy. So, we can use plain logics behind this concept. Therefore, we can use nested loops to implement this. After that, we can swap the position of rows and columns to get the new matrix. Then, the new matrix is generated.

### PROGRAM CODE:

Se our new Python code below:

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]

#### Explanation:

In this program, we have seen that we have used two for loops to implement this. So, the time complexity of the program is O(n^2). But, we can reduce the time complexity with the help of the function called transpose() present in the NumPy library.

For details, you can check this article.

## Leave a Reply