Convert between NumPy array and Python list
This article explains how to convert between a NumPy array (ndarray
) and a Python built-in list (list
).
Although the term "convert" is used for simplicity, the process actually involves creating a new object of a different type, while the original object remains unchanged.
The NumPy version used in this article is as follows. Note that functionality may vary between versions.
import numpy as np
print(np.__version__)
# 1.26.1
Convert a list to a NumPy array: np.array()
You can convert a list to a NumPy array (ndarray
) by passing a list to np.array()
.
l_1d = [0, 1, 2]
a_1d = np.array(l_1d)
print(a_1d)
# [0 1 2]
print(type(a_1d))
# <class 'numpy.ndarray'>
print(a_1d.dtype)
# int64
The data type (dtype
) of the generated ndarray
is automatically determined from the original list, but it can also be specified using the dtype
argument.
For more information about data type (dtype
) in NumPy, see the following article.
a_1d_f = np.array(l_1d, dtype=float)
print(a_1d_f)
# [0. 1. 2.]
print(a_1d_f.dtype)
# float64
The same applies to multi-dimensional arrays of two or more dimensions.
l_2d = [[0, 1, 2], [3, 4, 5]]
a_2d = np.array(l_2d)
print(a_2d)
# [[0 1 2]
# [3 4 5]]
Since multi-dimensional arrays represented in Python lists are simply nested lists (lists of lists), the number of elements in each nested list can vary.
However, directly passing these to np.array()
results in a ValueError
in NumPy version 1.24 and later.
- NumPy 1.24 Release Notes - Expired deprecations — NumPy v1.26 Manual
- NEP 34 — Disallow inferring dtype=object from sequences — NumPy Enhancement Proposals
l_2d_jagged = [[0, 1, 2], [3, 4]]
# a_2d_jagged = np.array(l_2d_jagged)
# ValueError: setting an array element with a sequence.
# The requested array has an inhomogeneous shape after 1 dimensions.
# The detected shape was (2,) + inhomogeneous part.
By setting dtype=object
, it is possible to create an ndarray
whose elements are lists of varying lengths. Before version 1.24, this was the default behavior.
a_2d_jagged = np.array(l_2d_jagged, dtype=object)
print(a_2d_jagged)
# [list([0, 1, 2]) list([3, 4])]
print(a_2d_jagged.shape)
# (2,)
print(a_2d_jagged.dtype)
# object
print(a_2d_jagged[0])
# [0, 1, 2]
print(type(a_2d_jagged[0]))
# <class 'list'>
Convert a NumPy array to a list: tolist()
You can convert a NumPy array (ndarray
) to a list with the tolist()
method of ndarray
.
The tolist()
method returns a nested list structure that mirrors the number of dimensions in the original ndarray
.
In the following examples, the NumPy arrays are created using arange()
and reshape()
.
- numpy.arange(), linspace(): Generate ndarray with evenly spaced values
- NumPy: How to use reshape() and the meaning of -1
For one dimension:
a_1d = np.arange(3)
print(a_1d)
# [0 1 2]
l_1d = a_1d.tolist()
print(l_1d)
# [0, 1, 2]
For two dimensions:
a_2d = np.arange(6).reshape((2, 3))
print(a_2d)
# [[0 1 2]
# [3 4 5]]
l_2d = a_2d.tolist()
print(l_2d)
# [[0, 1, 2], [3, 4, 5]]
For three dimensions:
a_3d = np.arange(24).reshape((2, 3, 4))
print(a_3d)
# [[[ 0 1 2 3]
# [ 4 5 6 7]
# [ 8 9 10 11]]
#
# [[12 13 14 15]
# [16 17 18 19]
# [20 21 22 23]]]
l_3d = a_3d.tolist()
print(l_3d)
# [[[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11]], [[12, 13, 14, 15], [16, 17, 18, 19], [20, 21, 22, 23]]]
Each element can be accessed by repeating the index [n]
.
print(l_3d[0])
# [[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11]]
print(l_3d[0][0])
# [0, 1, 2, 3]
print(l_3d[0][0][0])
# 0
Note that for zero-dimensional arrays (scalars), the result is not a list, but rather an object of the corresponding Python type, like an integer (int
) or a floating-point number (float
).
a_0d = np.array(100)
print(a_0d)
# 100
print(type(a_0d))
# <class 'numpy.ndarray'>
i = a_0d.tolist()
print(i)
# 100
print(type(i))
# <class 'int'>