When working with NumPy arrays in Python, you might encounter the error message “ValueError: setting an array element with a sequence.” This error typically occurs when you’re trying to assign a sequence (like a list) to a single element in a NumPy array.
In this article, I’ll explain what causes this error and show you multiple methods to fix it. After 10+ years of Python development, I’ve encountered this issue many times, especially when working with data science projects.
Let’s dive into understanding and resolving this common NumPy error.
Understand the Error
The error occurs when you try to assign a sequence to a single position in a NumPy array, but the shape doesn’t match what NumPy expects.
Here’s a simple example that triggers the error:
import numpy as np
# Create a numpy array
arr = np.zeros(5)
# Try to set a single element to a list
arr[0] = [1, 2, 3] # This causes the errorWhen you run this code, you’ll get:
ValueError: setting an array element with a sequenceThis happens because NumPy arrays are homogeneous and have a fixed shape. You can’t put a sequence into a single element position of a 1D array.
Read Repeat Arrays N Times in Python NumPy
Method 1: Use the Correct Array Shape
The simplest solution is to create a Python array with the correct shape from the beginning:
import numpy as np
# Create a 2D array that can hold sequences
arr = np.zeros((5, 3))
# Now you can assign a sequence to a row
arr[0] = [1, 2, 3] # This works!
print(arr)Output:
[[1. 2. 3.]
[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]]I executed the above example code and added the screenshot below.

This way, you’re assigning a sequence to a row that has the same length as your sequence.
Check out NumPy Array to a String in Pyt
Method 2: Use dtype=object
If you need to store sequences of different lengths in your Python array, you can use dtype=object:
import numpy as np
# Create an array of Python objects
arr = np.zeros(5, dtype=object)
# Now you can assign sequences to elements
arr[0] = [1, 2, 3]
arr[1] = [4, 5]
print(arr)Output:
[list([1, 2, 3]) list([4, 5]) 0 0 0]I executed the above example code and added the screenshot below.

This works because the array now stores Python objects rather than numeric values. However, be aware that object arrays don’t provide the same performance benefits as typical NumPy arrays.
Read Create a Matrix in Python
Method 3: Reshape Your Data
Sometimes the error occurs when you’re trying to append data to an array. In such cases, reshaping your data can help:
import numpy as np
# Starting array
arr = np.array([[1, 2, 3], [4, 5, 6]])
# New data
new_data = [7, 8, 9]
# Correctly append by reshaping
arr = np.vstack([arr, np.array([new_data])])
print(arr)Output:
[[1 2 3]
[4 5 6]
[7 8 9]]I executed the above example code and added the screenshot below.

The key here is to make sure the new data has the same shape as a row in your existing array.
Check out Random Number Between Two Values in Numpy
Method 4: Use numpy.append() or numpy.concatenate()
For appending data to an array in Python, NumPy provides dedicated functions:
import numpy as np
# Starting array
arr = np.array([[1, 2, 3], [4, 5, 6]])
# New data
new_data = np.array([[7, 8, 9]])
# Using numpy.append
arr_appended = np.append(arr, new_data, axis=0)
# Or using numpy.concatenate
arr_concatenated = np.concatenate((arr, new_data), axis=0)
print(arr_appended)These functions require that the dimensions match along the axis where you’re appending.
Common Scenarios Causing ValueError
Now, I will show you some common scenarios that cause ValueError.
Read Create a Python Empty Matrix
1. Mix Data Types in Scientific Computing
When working with scientific data, it’s common to accidentally try to insert a sequence into a simple array:
import numpy as np
# Temperature readings for New York (single values)
temperatures = np.array([68, 72, 65, 70, 71])
# Trying to add daily min/max for Chicago
temperatures[5] = [65, 78] # Error!Solution: Use a 2D array instead:
# Better approach
temp_data = np.zeros((6, 2)) # 6 days, min/max temps
temp_data[0] = [68, 75] # New York day 1 min/max
temp_data[5] = [65, 78] # Chicago day 1 min/max2. Build Machine Learning Features
When creating feature arrays for machine learning:
import numpy as np
# Creating a feature array for a model
features = np.zeros(100)
# Trying to add multiple features for one sample
features[0] = [age, income, credit_score] # Error!Solution: Create a proper 2D array:
# Proper approach
features = np.zeros((100, 3)) # 100 samples, 3 features each
features[0] = [age, income, credit_score] # Works!Check out NumPy Reset Index of an Array in Python
Best Practices to Avoid ValueError
- Plan your array shapes in advance: Before creating a NumPy array, think about what kind of data you’ll need to store in it.
- Use appropriate dimensions: If you need to store sequences, create a multi-dimensional array.
- Check shapes before assignment: Use
array.shapeto verify dimensions before assigning values. - Use broadcasting when possible: For more complex operations, NumPy’s broadcasting can help manipulate arrays of different shapes.
- Consider using lists for heterogeneous data: If you need collections of varying types or sizes, Python lists might be more appropriate than NumPy arrays.
Debugging Tips
If you encounter this error and aren’t sure why, try these debugging steps:
- Print the shape and dtype of your array:
print(arr.shape, arr.dtype) - Print the type and shape of the data you’re trying to assign:
print(type(data), np.array(data).shape) - Use a try-except block to identify problematic data:
try:
arr[index] = data
except ValueError as e:
print(f"Error at index {index} with data {data}")
print(f"Array shape: {arr.shape}, Data shape: {np.array(data).shape}")
raise eNumPy errors like “setting an array element with a sequence” can be frustrating at first, but once you understand the underlying concepts of array shapes and dimensions, they become much easier to handle. The key is ensuring that the shape of the data you’re trying to assign matches what NumPy expects at that position in the array.
I hope this article has helped you understand and resolve this common NumPy error. The methods that I have explained in this tutorial use the correct array shape, use dtype=object, reshape your data, and use numpy.append() or numpy.concat().
Other Python articles you may also like:

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.