note.nkmk.me

Convert 1D array to 2D array in Python (numpy.ndarray, list)

Posted: 2021-09-12 / Tags: Python, NumPy, List

This article explains how to convert a one-dimensional array to a two-dimensional array in Python, both for NumPy arrays ndarray and for built-in lists list.

  • Convert a one-dimensional numpy.ndarray to a two-dimensional numpy.ndarray
  • Convert a one-dimensional list to a two-dimensional list
    • With NumPy
    • Without NumPy

On the contrary, see the following article on how to convert (= flatten) a multi-dimensional array to one-dimensional array.

Sponsored Link

Convert a one-dimensional numpy.ndarray to a two-dimensional numpy.ndarray

Use the reshape() method to transform the shape of a NumPy array ndarray. Any shape transformation is possible, not limited to the transformation from a one-dimensional array to a two-dimensional array.

By using -1, the size of the dimension is automatically calculated.

import numpy as np

a = np.arange(6)
print(a)
# [0 1 2 3 4 5]

print(a.reshape(2, 3))
# [[0 1 2]
#  [3 4 5]]

print(a.reshape(-1, 3))
# [[0 1 2]
#  [3 4 5]]

print(a.reshape(2, -1))
# [[0 1 2]
#  [3 4 5]]

If you specify a shape that cannot be converted, an error occurs.

# print(a.reshape(3, 4))
# ValueError: cannot reshape array of size 6 into shape (3,4)

# print(a.reshape(-1, 4))
# ValueError: cannot reshape array of size 6 into shape (4)

Convert a one-dimensional list to a two-dimensional list

With NumPy

With NumPy, you can convert list to numpy.ndarray and transform the shape with reshape(), and then return it to list.

l = [0, 1, 2, 3, 4, 5]

print(np.array(l).reshape(-1, 3).tolist())
# [[0, 1, 2], [3, 4, 5]]

print(np.array(l).reshape(3, -1).tolist())
# [[0, 1], [2, 3], [4, 5]]

See the following article on how to convert numpy.ndarray and list to each other.

Without NumPy

Without NumPy, you can use list comprehensions, range(), and slices as follows.

def convert_1d_to_2d(l, cols):
    return [l[i:i + cols] for i in range(0, len(l), cols)]

l = [0, 1, 2, 3, 4, 5]

print(convert_1d_to_2d(l, 2))
# [[0, 1], [2, 3], [4, 5]]

print(convert_1d_to_2d(l, 3))
# [[0, 1, 2], [3, 4, 5]]

print(convert_1d_to_2d(l, 4))
# [[0, 1, 2, 3], [4, 5]]

The first argument is the original list, and the second argument is the number of elements of the inner list (= number of columns). If there is a remainder, a list with a different number of elements will be stored, as in the last example.

If you want to specify the number of rows, you can do as follows.

def convert_1d_to_2d_rows(l, rows):
    return convert_1d_to_2d(l, len(l) // rows)

print(convert_1d_to_2d_rows(l, 2))
# [[0, 1, 2], [3, 4, 5]]

print(convert_1d_to_2d_rows(l, 3))
# [[0, 1], [2, 3], [4, 5]]

print(convert_1d_to_2d_rows(l, 4))
# [[0], [1], [2], [3], [4], [5]]

The function in this example is just a simple one. If not divisible, the result is different from the specified number of rows, as in the last example.

Sponsored Link
Share

Related Categories

Related Articles