Grammar/Environments List

[Python] How to Transpose List [convert rows to columns]

This article shows how to transpose list.

Contents

  • [Appendix] How to display a two-dimensional array
  • Transpose a two-dimensional array by "for loop".
  • Transpose a two-dimensional array by "numpy"

sponsored link

How to display a two-dimensional array

In the beginning, I show how to display a two-dimensional array.

Displaying a two-dimensional array as a matrix makes it easier to visualize transposition.

If you try to display two-dimensional array by print( ) function , it is not displayed like a matrix.

matrix = [
[11, 12, 13, 14],
[21, 22, 23, 24],
[31, 32, 33, 34],
]

print(matrix)
# [[11, 12, 13, 14], [21, 22, 23, 24], [31, 32, 33, 34]]

This makes it difficult to imagine transposition.

I show two ways to display a two-dimensional array as a matrix.

How to display a two-dimensional array as a matrix by using "for loop"

I show how to display a two-dimensional array as a matrix by using "for loop".

matrix = [
[11, 12, 13, 14],
[21, 22, 23, 24],
[31, 32, 33, 34],
]

for i in range(len(matrix)):
    print(matrix[i])
"""
[11, 12, 13, 14]
[21, 22, 23, 24]
[31, 32, 33, 34]
"""

How to display a two-dimensional array as a matrix by using "Unpacking" and delimiter "\n"

You can unpack the elements by appending " * " to list.

Unpacking list elements allows them to be separated by a delimiter when they are displayed by using print( ) function.

By specifying "\n" as the delimiter, a two-dimensional array can be displayed as a matrix.

matrix = [
[11, 12, 13, 14],
[21, 22, 23, 24],
[31, 32, 33, 34],
]

print(*matrix, sep='\n')
"""
[11, 12, 13, 14]
[21, 22, 23, 24]
[31, 32, 33, 34]
"""

sponsored link

Transpose a two-dimensional array by "for loop"

I show how to transpose a two-dimensional array by "for loop".

Here is an example code.

matrix = [
[11, 12, 13, 14],
[21, 22, 23, 24],
[31, 32, 33, 34],
]

matrix_t = []

for i in range(len(matrix[0])) :
    tmp = []
    for v in matrix :
        tmp.append(v[i])
    matrix_t.append(tmp)

print(*matrix_t, sep='\n')
"""
[11, 21, 31]
[12, 22, 32]
[13, 23, 33]
[14, 24, 34]
"""

" for v in matrix " extracts rows in matrix one by one.

[ 11, 12, 13, 14 ], [ 21, 22, 23, 24 ], [ 31, 32, 33, 34 ] are taken out in order.

If i = 0, then v[0], so the 0th element of each row is added to "tmp" list by "append" method.

That is, "tmp" list contain [ 11, 21, 31 ].

Transpose a two-dimensional array by "numpy"

This is the easiest way.

A numerical library called "numpy" can be used to easily transpose two-dimensional arrays.

After converting a list to a numpy type, a two-dimensional array is transposed by simply appending ".T" to it.

import numpy as np

matrix = [
[11, 12, 13, 14],
[21, 22, 23, 24],
[31, 32, 33, 34],
]

print(type(matrix))
# <class 'list'>

matrix_np = np.array(matrix)
print(type(matrix_np))
# <class 'numpy.ndarray'>

matrix_np_t = matrix_np.T
print(matrix_np_t)
"""
[[11 21 31]
[12 22 32]
[13 23 33]
[14 24 34]
"""

matrix_np_t_list = matrix_np_t.tolist()
print(type(matrix_np_t_list))
# <class 'list'>

In the part "matrix_np_t_list = matrix_np_t.tolist()", an array of numpy type is converted back to list type.

sponsored link

-Grammar/Environments, List
-,