How to Fix ValueError: setting an array element with a sequence
When working with NumPy, one common and sometimes confusing error you may encounter is ValueError: setting an array element with a sequence. This error typically arises when you attempt to create or modify a NumPy array but the data types of the elements you provide don’t align with the expected data type of the array.
Why does this error occur ?
This error occurs when NumPy expects a single value like a number or string but receives a sequence like a list or array instead. It often happens with:
- Mixed data types in the input list
- Uneven nested lists
- Assigning a list to an element that expects a scalar
NumPy cannot automatically convert these sequences into a fixed, uniform structure so it raises a ValueError.
Common Situations and How to Fix the Error
1. Using common data-type
When you try to create a NumPyarray from a list containing both numbers and nested lists and specify a strict data type like int, it causes a ValueError.
import numpy as np
a = [1, 2, 4, [5, [6, 7]]]
b = np.array(a, dtype=int)
Output
Hangup (SIGHUP)
Traceback (most recent call last):
File "/home/guest/sandbox/Solution.py", line 4, in <module>
b = np.array(a, dtype=int)
ValueError: setting an array element with a sequence.
Solution: Use a more flexible data type like object which supports mixed types and nested lists.
import numpy as np
a = [1, 2, 4, [5, [6, 7]]]
b = np.array(a, dtype=object)
print(b)
Output
[1 2 4 list([5, [6, 7]])]
2. Assigning list to string
If the array expects strings dtype=str, but you try to assign a list to an element, it causes a ValueError.
import numpy as np
a = np.array(["Geeks", "For"], dtype=str)
a[1] = ["for", "Geeks"]
Output
Hangup (SIGHUP)
Traceback (most recent call last):
File "/home/guest/sandbox/Solution.py", line 4, in <module>
arr[1] = ["for", "Geeks"]
~~~^^^
ValueError: setting an array element with a sequence
Solution: Make sure the element assigned matches the data type. If you need to assign a list, create the array with dtype=object or convert the list to a string.
import numpy as np
# Use dtype=object
a = np.array(["Geeks", "For"], dtype=object)
a[1] = ["for", "Geeks"]
print(a)
# Convert list to string
a = np.array(["Geeks", "For"], dtype=str)
a[1] = str(["for", "Geeks"])
print(a)
Output
['Geeks' list(['for', 'Geeks'])] ['Geeks' "['for"]
3. Unequal nested lists
If the array expects strings dtype=str , but you try to assign a list to an element, it causes a ValueError.
import numpy as np
a = [[1, 2], [3, 4, 5]]
np_array = np.array(a, dtype=int)
Output
Hangup (SIGHUP)
Traceback (most recent call last):
File "/home/guest/sandbox/Solution.py", line 4, in <module>
np_array = np.array(a, dtype=int)
ValueError: setting an array element with a sequence.
Solution: Use dtype=object to allow arrays of nested lists with different lengths, or pad the nested lists to equal lengths before creating the array.
import numpy as np
a = [[1, 2], [3, 4, 5]]
res = np.array(a, dtype=object)
print(res)
Output
[list([1, 2]) list([3, 4, 5])]
4. Assigning list to number
When you assign a list to a position in an array that expects a single number e.g., int , it raises a TypeError.
import numpy as np
arr = np.zeros((3, 3), dtype=int)
arr[0, 0] = [1, 2]
Output
Hangup (SIGHUP)
Traceback (most recent call last):
File "/home/guest/sandbox/Solution.py", line 4, in <module>
arr[0, 0] = [1, 2]
~~~^^^^^^
TypeError: int() argument must be a string, a bytes-like object or a real number, not 'list'
Solution: Assign only single numeric values to array elements with numeric data types. If you want to store lists, use dtype=object.
import numpy as np
a = np.zeros((3, 3), dtype=int)
# Assign a single number
a[0, 0] = 1
# Use dtype=object
a_obj = np.zeros((3, 3), dtype=object)
a_obj[0, 0] = [1, 2]
print(a_obj)
Output
[[list([1, 2]) 0 0] [0 0 0] [0 0 0]]