Possible reason 1: trying to create a jagged array

You may be creating an array from a list that isn't shaped like a multi-dimensional array:

Copynumpy.array([[1, 2], [2, 3, 4]])         # wrong!
Copynumpy.array([[1, 2], [2, [3, 4]]])       # wrong!

In these examples, the argument to numpy.array contains sequences of different lengths. Those will yield this error message because the input list is not shaped like a "box" that can be turned into a multidimensional array.

Possible reason 2: providing elements of incompatible types

For example, providing a string as an element in an array of type float:

Copynumpy.array([1.2, "abc"], dtype=float)   # wrong!

If you really want to have a NumPy array containing both strings and floats, you could use the dtype object, which allows the array to hold arbitrary Python objects:

Copynumpy.array([1.2, "abc"], dtype=object)
Answer from Sven Marnach on Stack Overflow
Top answer
1 of 11
401

Possible reason 1: trying to create a jagged array

You may be creating an array from a list that isn't shaped like a multi-dimensional array:

Copynumpy.array([[1, 2], [2, 3, 4]])         # wrong!
Copynumpy.array([[1, 2], [2, [3, 4]]])       # wrong!

In these examples, the argument to numpy.array contains sequences of different lengths. Those will yield this error message because the input list is not shaped like a "box" that can be turned into a multidimensional array.

Possible reason 2: providing elements of incompatible types

For example, providing a string as an element in an array of type float:

Copynumpy.array([1.2, "abc"], dtype=float)   # wrong!

If you really want to have a NumPy array containing both strings and floats, you could use the dtype object, which allows the array to hold arbitrary Python objects:

Copynumpy.array([1.2, "abc"], dtype=object)
2 of 11
95

The Python ValueError:

ValueError: setting an array element with a sequence.

Means exactly what it says, you're trying to cram a sequence of numbers into a single number slot. It can be thrown under various circumstances.

1. When you pass a python tuple or list to be interpreted as a numpy array element:

Copyimport numpy

numpy.array([1,2,3])               #good

numpy.array([1, (2,3)])            #Fail, can't convert a tuple into a numpy 
                                   #array element


numpy.mean([5,(6+7)])              #good

numpy.mean([5,tuple(range(2))])    #Fail, can't convert a tuple into a numpy 
                                   #array element


def foo():
    return 3
numpy.array([2, foo()])            #good


def foo():
    return [3,4]
numpy.array([2, foo()])            #Fail, can't convert a list into a numpy 
                                   #array element

2. By trying to cram a numpy array length > 1 into a numpy array element:

Copyx = np.array([1,2,3])
x[0] = np.array([4])         #good



x = np.array([1,2,3])
x[0] = np.array([4,5])       #Fail, can't convert the numpy array to fit 
                             #into a numpy array element

A numpy array is being created, and numpy doesn't know how to cram multivalued tuples or arrays into single element slots. It expects whatever you give it to evaluate to a single number, if it doesn't, Numpy responds that it doesn't know how to set an array element with a sequence.

🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-fix-valueerror-setting-an-array-element-with-a-sequence
How to Fix ValueError: setting an array element with a sequence - GeeksforGeeks
July 23, 2025 - 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: ... NumPy cannot automatically convert these sequences into a fixed, uniform structure so ...
Discussions

ValueError: setting an array element with a sequence
Im trying to training a Random Forest Classifier to predict movie success based on various features. Im using tmdb_5000_movies.csv data set. code as below df_movies = pd.read_csv(‘tmdb_5000_movies.csv’) df_credits= pd.read_csv(‘tmdb_5000_credits.csv’) df_movies.rename(columns={‘id’: ... More on discuss.python.org
🌐 discuss.python.org
4
0
June 9, 2024
ValueError: setting an array element with a sequence
Summary I am trying to deploy my model but I am getting this error listed in the title. I tried removing brackets from the list in line 110 and that didn’t work. Steps to reproduce Code snippet: Traceback (most recent call last): File "/home/appuser/venv/lib/python3.9/site-packages/strea... More on discuss.streamlit.io
🌐 discuss.streamlit.io
1
0
March 29, 2023
machine learning - Getting 'ValueError: setting an array element with a sequence.' when attempting to fit mixed-type data - Data Science Stack Exchange
I have already seen this, this and this question, but none of the suggestions seemed to fix my problem (so I have reverted them). I have the following code: nlp = spacy.load('en_core_web_sm') par... More on datascience.stackexchange.com
🌐 datascience.stackexchange.com
June 30, 2019
"ValueError: setting an array element with a sequence." when integrating with odeint
The value error says you are trying to set an element in an array with a sequence which isn't permitted, does your traceback say what line this error occurs on, I see several places this could be happening. More on reddit.com
🌐 r/learnpython
12
1
December 17, 2021
🌐
Reddit
reddit.com › r/learnpython › "valueerror: setting an array element with a sequence." when integrating with odeint
r/learnpython on Reddit: "ValueError: setting an array element with a sequence." when integrating with odeint
December 17, 2021 -

I'm trying to integrate some ordinary differential equations but I need some of the parameters to be a cosine function of time. While testing the code, I keep getting the above value error. I also posted this to r/learningpython but this sub seems like it may be more helpful!

Here's the code:

import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import odeint
def vd(t):
return -(267 / 1440) * np.cos((2 * np.pi) / 365 * t) + (639 / 1440)

tmax = 2 * 365 # how long to run the system in days
t = np.arange(0.0, tmax, 1.0)

# define the constants
N = 3.295e8 # population of the US
theta = 0.8 # infectiousness
zeta = 0.75 # susceptibility
c = (N * 10 ** -5) / N # contact rate, depending on percentage of total population
alpha = theta * zeta * c
gamma = 0.1 # recovery rate, used often in class
rho = 0.2 # rate of loss of immunity
deltad = 0.1*vd(t) # rate of loss of Vitamin-D in body
# these are all the differential equations from simple SIR model
def deriv(y, t, alpha, gamma, rho):
S, I, R, D = y
dS = -alpha * I * S + rho * R
dI = alpha * I * S - gamma * I
dR = gamma * I - rho * R - rho * R * deltad
dD = deltad * (rho * R - S * L(t))
return dS, dI, dR, dD
# define some more initial conditions for integration
I0 = 1 # 1st person infected
R0 = 0 # 0 people recovered at first, obviously
S0 = N - I0 - R0
D0 = 0 # initial vitamin D in population, t=0 means January 1st
# initial conditions vector
y0 = S0, I0, R0, D0

ret = odeint(deriv, y0, t, args=(alpha, gamma, rho))
S, I, R, D = ret.T

Then I try to plot S, I, R, and D but that error pops up regarding the odeint line.

Those constants will probably change but I do need to input some stuff into dS, dI, dR, and dD that look like deltad and was hoping that someone on here with more python experience could help me.

Thank you!

🌐
Python.org
discuss.python.org › python help
ValueError: setting an array element with a sequence - Python Help - Discussions on Python.org
June 9, 2024 - Im trying to training a Random Forest Classifier to predict movie success based on various features. Im using tmdb_5000_movies.csv data set. code as below df_movies = pd.read_csv(‘tmdb_5000_movies.csv’) df_credits= pd.read_csv(‘tmdb_5000_credits.csv’) df_movies.rename(columns={‘id’: ...
🌐
Squash
squash.io › how-to-fix-valueerror-setting-an-array-element-with-a-sequence-in-python
Fixing "ValueError: Setting Array with a Sequenc" In Python
November 2, 2023 - The 'ValueError: Setting an Array Element with a Sequence' error occurs in Python when you try to assign a sequence (such as a list or another array) to a single element of a NumPy array.
🌐
Statology
statology.org › home › how to fix: valueerror: setting an array element with a sequence
How to Fix: ValueError: setting an array element with a sequence
October 22, 2021 - The way to fix this error is to simply assign one value into the first position of the array: #assign the value '4' to the first position of the array data[0] = np.array([4]) #view updated array data array([ 4, 2, 3, 4, 5, 6, 7, 8, 9, 10])
🌐
Finxter
blog.finxter.com › home › learn python blog › [fixed] valueerror: setting an array element with a sequence
[FIXED] ValueError: setting an array element with a sequence - Be on the Right Side of Change
August 24, 2023 - Introduction In this article, we will look at how you can set an array element with a sequence, and then we will also learn the ways to solve the error – “ValueError: setting an array element with a sequence“. In Python, the ValueError generally gets raised when a function gets the argument of the right ...
Find elsewhere
🌐
STechies
stechies.com › valueerror-setting-array-element-sequence
ValueError: setting an array element with a sequence
This error usually occurs when the Numpy array is not in sequence. Let us see the details of this error and also its solution: ... Traceback (most recent call last): File "pyprogram.py", line 2, in <module> np.array([[[1, 2], [3, 4], [5, 6]], ...
🌐
Arab Psychology
scales.arabpsychology.com › home › how to easily fix “valueerror: setting an array element with a sequence
How To Easily Fix "ValueError: Setting An Array Element With A Sequence
December 3, 2025 - If this is the goal, the array must be initialized to hold generic Python objects by setting its data type to dtype=object. By using dtype=object, NumPy relinquishes its fast, contiguous memory blocks and instead stores pointers to arbitrary ...
🌐
Python Pool
pythonpool.com › home › blog › [solved] valueerror: setting an array element with a sequence easily
[Solved] ValueError: Setting an Array Element With A Sequence Easily
May 4, 2021 - In python, we often encounter the error as ValueError: setting an array element with a sequence is when we are working with the numpy library.
🌐
GoLinuxCloud
golinuxcloud.com › home › programming › fix "setting an array element with a sequence" in python (numpy valueerror explained + examples)
Fix "Setting an Array Element with a Sequence" in Python (NumPy ValueError Explained + Examples) | GoLinuxCloud
April 1, 2026 - This error occurs when NumPy receives inconsistent data, such as assigning a sequence (list/array) where a scalar is expected or when array shapes do not match. ... NumPy expects all elements to follow the same structure (shape), but you are ...
🌐
Streamlit
discuss.streamlit.io › random
ValueError: setting an array element with a sequence - Random - Streamlit
March 29, 2023 - Summary I am trying to deploy my model but I am getting this error listed in the title. I tried removing brackets from the list in line 110 and that didn’t work. Steps to reproduce Code snippet: Traceback (most recent…
🌐
Python Guides
pythonguides.com › valueerror-setting-an-array-element-with-a-sequence
ValueError: Setting An Array Element With A Sequence Error In Python
May 16, 2025 - You can’t put a sequence into a single element position of a 1D array. ... 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) ... 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. ... 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)
🌐
Position Is Everything
positioniseverything.net › home › valueerror: setting an array element with a sequence.: fix it now
Valueerror: Setting an Array Element With a Sequence.: Fix It Now - Position Is Everything
October 7, 2025 - The valueerror setting an array element with a sequence. problem is fixed by equaling the length on both arrays. Follow these steps to debug the error.
🌐
Edureka Community
edureka.co › home › community › categories › python › valueerror setting an array element with a...
ValueError setting an array element with a sequence | Edureka Community
April 30, 2022 - I am getting this error: File "mypath\mypythonscript.py", line 3484, in secondfunction RRDuringArray = ... explain to me how to fix this problem?
🌐
Delft Stack
delftstack.com › home › howto › python › python setting an array element with a sequence
How to Fix ValueError: Setting an Array Element With a Sequence in Python | Delft Stack
February 2, 2024 - In Python, the ValueError: setting an array element with a sequence occurs when you try to assign an invalid data type to an array. This can also occur when you try to assign multiple values to a single location on the array.
🌐
DEV Community
dev.to › itsmycode › python-valueerror-setting-an-array-element-with-a-sequence-49eh
Python ValueError: setting an array element with a sequence - DEV Community
September 8, 2021 - In Python, if you are mainly working with numpy and creating a multi-dimensional array, you would have encountered valueerror: setting an array element with a sequence.
🌐
W3docs
w3docs.com › python
ValueError: setting an array element with a sequence
import numpy as np # Creating a 2D array arr = np.array([[1, 2], [3, 4]]) # Try to set an element with a sequence try: arr[0][0] = [5, 6] except ValueError as e: print(e)
🌐
Codingem
codingem.com › home › numpy fix “valueerror: setting an array element with a sequence”
Numpy Fix "ValueError: setting an array element with a sequence"
July 10, 2025 - This guide teaches you how to fix the common error ValueError: setting array element with a sequence in Python/NumPy.