HOWTO · NumPy
How to Convert NumPy Array to Tuple
Learn how to convert a NumPy array to a tuple in Python using tuple() and map(), with examples for one-dimensional and two-dimensional arrays.
On this page
Convert a NumPy array with Python’s built-in tuple() function. For a one-dimensional array, tuple(array) creates a tuple of its elements. For a two-dimensional array, convert each row as well when you need nested tuples. The examples below show both shapes and a map() alternative.
Convert a 1D NumPy array to a tuple
tuple() consumes an iterable and puts its yielded items into a tuple. A one-dimensional NumPy array yields its values when iterated, so passing it directly to tuple() gives a flat tuple. The following example uses NumPy integers; their displayed representation can depend on the NumPy version.
Run this example:
import numpy as np
array = np.array([10, 20, 30])
result = tuple(array)
print(result)
(np.int64(10), np.int64(20), np.int64(30))
Convert a 2D NumPy array to nested tuples
Iteration over a two-dimensional array yields rows. Consequently, tuple(matrix) makes an outer tuple whose items are still NumPy row arrays. To convert the rows too, pass each row to tuple(). This keeps the original row and column grouping in the result.
import numpy as np
matrix = np.array([[1, 2], [3, 4]])
result = tuple(tuple(row) for row in matrix)
print(result)
((np.int64(1), np.int64(2)), (np.int64(3), np.int64(4)))
The comprehension performs the inner conversion once for every row, then the outer tuple() collects those row tuples. NumPy scalar display can vary by version.
Use map() to convert each row
You can express the same row-wise operation with map(tuple, matrix). In Python, map() returns an iterator, so wrap it in tuple() to materialize all converted rows. This is a concise alternative when the operation is simply applying tuple() to each row.
import numpy as np
matrix = np.array([[1, 2], [3, 4]])
result = tuple(map(tuple, matrix))
print(result)
((np.int64(1), np.int64(2)), (np.int64(3), np.int64(4)))
Here map() converts rows, not each scalar value. For an element-wise value conversion, use a different expression, such as tuple(int(value) for value in array) when converting a one-dimensional array’s values to Python integers. The choice of conversion depends on whether you want to change rows or values.
Choose a conversion that matches the result you need
Use tuple(array) for a flat, one-dimensional sequence. For a two-dimensional array, use a nested conversion such as tuple(tuple(row) for row in matrix) or its map() equivalent when each row should remain a separate tuple. These forms preserve the grouping of values across rows.
Tuples do not keep NumPy array properties such as the shape attribute or array operations. Their nesting reflects the converted rows, but it is not NumPy shape metadata. Flattening with an array method is an optional choice when your task needs one sequence of all values; it changes the structure by discarding row grouping. It is not needed to create nested tuples.
Also, converting an array to a tuple does not guarantee that every item becomes a native Python scalar. NumPy values may remain NumPy scalar objects. Apply an explicit conversion such as int() only when the downstream code requires that type, and choose a conversion suitable for the actual values. The displayed NumPy scalar representation can vary by version.