# Transpose 2D list in Python (swap rows and columns)

Posted: 2019-11-21 / Tags: Python, List

A two-dimensional array can be represented by a list of lists using the Python built-in `list` type.

Here are some ways to swap the rows and columns of this two-dimensional list.

• Convert to `numpy.ndarray` and transpose with `T`
• Convert to `pandas.DataFrame` and transpose with `T`
• Transpose with built-in function `zip()`

It is easier to use NumPy and pandas, but if you don't want to import NumPy or pandas just for transposition, you can use the `zip()` function.

The original two-dimensional list is defined as follows:

``````import numpy as np
import pandas as pd

l_2d = [[0, 1, 2], [3, 4, 5]]
``````

## Convert to numpy.ndarray and transpose with T

Create a NumPy array `ndarray` from the original 2D list and get the transposed object with the `T` attribute.

If you want a `list` type object, convert it to a `list` with the `tolist()` method.

``````arr_t = np.array(l_2d).T

print(arr_t)
print(type(arr_t))
# [[0 3]
#  [1 4]
#  [2 5]]
# <class 'numpy.ndarray'>

l_2d_t = np.array(l_2d).T.tolist()

print(l_2d_t)
print(type(l_2d_t))
# [[0, 3], [1, 4], [2, 5]]
# <class 'list'>
``````

In addition to the `T` attribute, you can also use the `transpose()` method of `ndarray` and the `numpy.transpose()` function. Please refer to the following post for details such as processing for multi-dimensional arrays more than three dimensions.

## Convert to pandas.DataFrame and transpose with T

Create `pandas.DataFrame` from the original 2D list and get the transposed object with the `T` attribute.

If you want a `list` type object, get `numpy.ndarray` with the `values` attribute and convert it to `list` with the `tolist ()` method.

``````df_t = pd.DataFrame(l_2d).T

print(df_t)
print(type(df_t))
#    0  1
# 0  0  3
# 1  1  4
# 2  2  5
# <class 'pandas.core.frame.DataFrame'>

l_2d_t = pd.DataFrame(l_2d).T.values.tolist()

print(l_2d_t)
print(type(l_2d_t))
# [[0, 3], [1, 4], [2, 5]]
# <class 'list'>
``````

## Transpose with built-in function `zip()`

You can transpose a two-dimensional list using the built-in function `zip()`.

`zip()` is a function that returns an iterator that summarizes the multiple iterables (`list`, `tuple`, etc.).

In addition, use `*` that allows you to expand the list and pass its elements to the function.

``````l_2d_t_tuple = list(zip(*l_2d))

print(l_2d_t_tuple)
print(type(l_2d_t_tuple))
# [(0, 3), (1, 4), (2, 5)]
# <class 'list'>

print(l_2d_t_tuple)
print(type(l_2d_t_tuple))
# (0, 3)
# <class 'tuple'>
``````

Elements are `tuple`. If you want to make `list`, use `list()` and list comprehensions.

``````l_2d_t = [list(x) for x in zip(*l_2d)]

print(l_2d_t)
print(type(l_2d_t))
# [[0, 3], [1, 4], [2, 5]]
# <class 'list'>

print(l_2d_t)
print(type(l_2d_t))
# [0, 3]
# <class 'list'>
``````

The detail is as follows.

The elements of the list are expanded with `*`, the expanded elements are combined with the `zip()` function, and `tuple` is converted to `list` in the list comprehension.

``````print(*l_2d)
# [0, 1, 2] [3, 4, 5]

print(list(zip([0, 1, 2], [3, 4, 5])))
# [(0, 3), (1, 4), (2, 5)]

print([list(x) for x in [(0, 3), (1, 4), (2, 5)]])
# [[0, 3], [1, 4], [2, 5]]
``````