Transpose 2D List in Python (3 Examples)

 

Hi! This tutorial will demonstrate how to rearrange a 2D list in the Python programming language.

Here is a quick overview:

Let’s jump into the Python code!

 

Create Sample 2D List

We will create the sample 2D list of integers that will be swapped or transposed in this tutorial. Therefore, in your preferred Python coding IDE, run the line of code below to create the sample 2D list:

original = [[1, 2], [3, 4], [5, 6]]
 
print(original)
 
# [[1, 2], [3, 4], [5, 6]]
 
print(type(original))
 
# <class 'list'>

With the sample 2D list created, we are now going to examine different ways to switch its arrangement.
 

Example 1: Rearrange 2D List Using Nested List Comprehension

In this first example, we are going to use nested list comprehension to rearrange the 2D list:

transposed = [[row[i] for row in original] for i in range(len(original[0]))]
 
print(transposed)
 
# [[1, 3, 5], [2, 4, 6]]

In the example above, we use a nested list comprehension to iterate over each element of the original matrix and construct a new matrix in which the rows and columns have been swapped.

The outer loop iterates over the columns of the original matrix, while the inner loop iterates over the rows. By using the range() function with the length of the first row of the original matrix, we ensure that the new matrix has the same number of columns as the original.
 

Example 2: Rearrange 2D List Using zip() Function

In this next example, we will use Python’s zip() function to swap the arrangement of the 2D list:

transposed = list(zip(*original))
 
print(transposed)
 
# [(1, 3, 5), (2, 4, 6)]

The zip() function takes any number of iterables as arguments and returns an iterator that aggregates elements from each iterable.

By passing the unpacking operator * before the original matrix, we unpack the rows of the matrix into separate arguments for the zip() function.

The result is a series of tuples, where each tuple contains the corresponding elements from the rows of the original matrix.
 

Example 3: Rearrange 2D List Using NumPy

In this final example, we will use the transpose() function from Python’s NumPy library to transpose the 2D list.

First, though, we need to install and import NumPy. Therefore, run the lines of code below to install and import NumPy:

# install NumPy
pip install numpy
 
# import NumPy
import numpy as np

Now that we have installed and imported NumPy into our Python environment, we can use the functions in the library. Let us now switch the arrangement of the 2D list:

transposed = np.transpose(original)
 
print(transposed)
 
# [[1 3 5]
# [2 4 6]]

In the above example, the transpose() function returns a new array with the axes switched. In the case of the 2D array like our list, the rows and columns have been swapped.

You will notice that all three examples return the same results, but in slightly different structures. Therefore, select the method that works best for your use case.

With that, we have demonstrated how to transpose or rearrange a 2D list in Python. I hope you found this tutorial helpful!

 

Video, Further Resources & Summary

Do you need more explanations on how to transpose a 2D list in Python? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.

In the video, we explain in some more detail how to transpose a 2D list in Python.

 

The YouTube video will be added soon.

 

Furthermore, I encourage you to check out other interesting Python list tutorials on Statistics Globe, starting with these ones:

This post has shown how to transpose a 2D list in Python. In case you have further questions, you may leave a comment below.

 

R & Python Expert Ifeanyi Idiaye

This page was created in collaboration with Ifeanyi Idiaye. You might check out Ifeanyi’s personal author page to read more about his academic background and the other articles he has written for the Statistics Globe website.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

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

Fill out this field
Fill out this field
Please enter a valid email address.

Top