Converting Data Structures to NumPy Arrays

Last Updated : 13 Feb, 2026

Converting Python data structures to NumPy arrays allows data to be processed using NumPy’s optimized and efficient array operations. NumPy arrays are widely used for numerical computing, data analysis and scientific applications.

Data Structures to NumPy Arrays

NumPy provides the np.array() function to convert different Python data structures into NumPy arrays. Below are common conversions explained with simple examples.

1. List: It store elements in an ordered manner, but they are not optimized for numerical computations. Converting a list to a NumPy array improves performance and enables vectorized operations.

Python
import numpy as np
a = [1, 2, 3]
arr = np.array(a)
print(arr)

Output
[1 2 3]

2. Tuple are immutable collections, meaning their elements cannot be changed. Converting a tuple to a NumPy array allows mathematical and numerical processing.

Python
import numpy as np
b = (1, 2, 3)
arr = np.array(b)
print(arr)

Output
[1 2 3]

3. Dictionary: It store data as key-value pairs. When converting a dictionary to a NumPy array, only the keys are considered unless specified otherwise.

Python
import numpy as np
d = {1: "a", 2: "b", 3: "c"}
arr = np.array(list(d.keys()))
print(arr)

Output
[1 2 3]

4. Set: It store unique elements but do not maintain a fixed order. Converting a set to a NumPy array requires converting it to a list first.

Python
import numpy as np
s = {1, 2, 3}
arr = np.array(list(s))
print(arr)

Output
[1 2 3]

5. String: It can be converted into NumPy arrays either as individual characters or as a single element.

Python
import numpy as np
arr = np.array(list("abc"))
print(arr)

Output
['a' 'b' 'c']

Converting Data Type of NumPy Arrays

NumPy allows you to change the data type of an array using the astype() method. This creates a new array with the desired data type without modifying the original one.

1. Integer Array to Other Data Types: Integer arrays can be converted into float, string or boolean arrays depending on the requirement.

Python
import numpy as np

a = np.array([1, 2, 3, 4, 5])
print(a)

b = a.astype(float)
print(b)

c = a.astype(str)
print(c)

d = a.astype(bool)
print(d)

Output
[1 2 3 4 5]
[1. 2. 3. 4. 5.]
['1' '2' '3' '4' '5']
[ True  True  True  True  True]

2. Float Array to Other Data Types: Float arrays are often converted to integers, strings or booleans for storage or logical operations.

Python
import numpy as np

a = np.array([1.0, 2.0, 3.0, 4.0, 5.0])
print(a)

b = a.astype(int)
print(b)

c = a.astype(str)
print(c)

d = a.astype(bool)
print(d)

Output
[1. 2. 3. 4. 5.]
[1 2 3 4 5]
['1.0' '2.0' '3.0' '4.0' '5.0']
[ True  True  True  True  True]

3. String Array to Other Data Types: String arrays containing numeric values can be converted into integers or floats.

Python
import numpy as np

a = np.array(['1', '2', '3', '4', '5'])
print(a)

b = a.astype(int)
print(b)

c = a.astype(float)
print(c)

d = a.astype(bool)
print(d)

Output
['1' '2' '3' '4' '5']
[1 2 3 4 5]
[1. 2. 3. 4. 5.]
[ True  True  True  True  True]

4. Boolean Array to Other Data Types: Boolean arrays can be converted into integers, floats, or strings.

Python
import numpy as np

a = np.array([True, False, False, False, True])
print(a)

b = a.astype(int)
print(b)

c = a.astype(float)
print(c)

d = a.astype(str)
print(d)

Output
[ True False False False  True]
[1 0 0 0 1]
[1. 0. 0. 0. 1.]
['True' 'False' 'False' 'False' 'True']
Comment