3 min read

Matrices are a multidimensional array of order `m*n`

where m is the number of rows and n number of columns. Matrices are handy to store data.

A matrix can be represented in Python row-wise, as a list of lists where each inner list represents one row of the matrix. For instance, the matrix

```
1 2 3
4 5 6
```

would be represented as `M = `

. Here, `[[1, 2, 3], [4, 5, 6]]``M[0]`

will return elements of the first row and `M[0][0]`

will give the first element of the first row and so on, note that similar to arrays indexing in matrix starts with 0.

Matrix created as a result of interchanging the rows and columns of a matrix is called Transpose of that Matrix, for instance, the transpose of the above matrix would be:

```
1 4
2 5
3 6
```

This transposed matrix can be written as [[1, 4], [2, 5], [3, 6]]. In Python, there is always more than one way to solve any problem.

In this article, we will learn how to programmatically find out the transpose of any given matrix.

```
def transpose(m):
rez = [[[m[j][i] for j in range(len(m))] for i in range(len(m[0]))]]
for row in rez:
return row
print("result=",transpose([[1, 3, 5], [2, 4, 6]]))
print("result=",transpose([[1, 1, 1], [2, 2, 2], [3, 3, 3]]))
print("result=",transpose([[1, 4, 9]]))
```

**Output:**

```
result= [[1, 2], [3, 4], [5, 6]]
result= [[1, 2, 3], [1, 2, 3], [1, 2, 3]]
result= [[1], [4], [9]]
```

The above program is using list comprehension to transpose the matrix, the list evaluating from the expression is the transpose of the given matrix.

Python comes with many inbuilt libraries zip is among those. The `zip()`

function returns an iterator of tuples based on the iterable object. In order to get the transpose of the matrix first, we need to unzip the list using `*`

operator then zip it.

```
def transpose(m):
t_matrix = [*zip(*m)]
return t_matrix
print("result=", transpose([[1, 3, 5], [2, 4, 6]]))
print("result=", transpose([[1, 1, 1], [2, 2, 2], [3, 3, 3]]))
print("result=", transpose([[1, 4, 9]]))
```

**Output**:

```
result= [(1, 2), (3, 4), (5, 6)]
result= [(1, 2, 3), (1, 2, 3), (1, 2, 3)]
result= [(1,), (4,), (9,)]
```

NumPy is an extremely popular library among data scientist heavily used for large computation of array, matrices and many more with Python.

NumPy comes with an inbuilt solution to transpose any matrix `numpy.matrix.transpose`

the function takes a numpy array and applies the transpose method.

Note that, Numpy doesn't come bundled with default Python installation, so you need to install it first. NumPy can be installed using the pip installer as follows.

`pip install numpy`

Once the installation is done executing the following program will return the transpose of a given matrix,

```
import numpy
def transpose(m):
m = numpy.array(m)
return m.transpose()
print("result: \n", transpose([[1, 3, 5], [2, 4, 6]]))
print("result: \n", transpose([[1, 1, 1], [2, 2, 2], [3, 3, 3]]))
print("result: \n", transpose([[1, 4, 9]]))
```

**Output:**

```
result:
[[1 2]
[3 4]
[5 6]]
result:
[[1 2 3]
[1 2 3]
[1 2 3]]
result:
[[1]
[4]
[9]]
```

PROGRAMS

Latest from djangocentral

The much-awaited pull request for an async-compatible interface to Queryset just got merged into the main branch of Django.Pull Request - https://github.com/django/django/pull/14843
The Django core team has been progressively adding async suppor…

Read more →

3 min read

Django admin is undoubtedly one of the most useful apps of Django. Over the years there has been very little change in the admin app as far as the UX is concerned and it's not a bad thing at all. Django admin was designed to provide a simple and minimali…

Read more →

4 min read