valueerror: setting an array element with a sequence – Python

Total
0
Shares

Python throws valueerror: setting an array element with a sequence, when you are trying to create an array with the list which is not proper multi-dimensional in shape. Another reason is related to the type of content in array. For example, you are defining a float array and inserting string values in it.

Consider this code example –

import numpy as np
print(np.array([[1, 2], [3, 4, 5]]))

This will throw valueerror setting array element with sequence, because we are asking numpy to create an array from the list which has elements of different dimensions – [1,2] and [3,4,5].

If we make the length of both arrays equal, then there will be no error. So, this code will work fine –

import numpy as np
print(np.array([[1, 2, 0], [3, 4, 5]]))

Another reason for the same problem is using a different type of element in array than the defined datatype. So, a float array can only contain float values and not strings. Check out this code –

import numpy as np
print(np.array([2.1, 2.2, "Ironman"], dtype=float))

Since Ironman is a string, so it can’t be the part of array of floats (dtype=float). If you want to keep it unrestricted, then use dtype=object.

import numpy as np
print(np.array([2.1, 2.2, "Ironman"], dtype=object))

Now we will look at various situations where we will or will not get the array element with sequence error –

These examples will work fine –

import numpy as np

print(np.array(["Ironman", "Thor", "Hulk"]))
import numpy as np

print(np.mean([10,(20 + 30)]))

Here, (20 + 30) will not act as tuple. Else, it will be the expression which result as 50.

import numpy as np

def superhero():
    return "Thor"

print(np.array(["Ironman", superhero()]))

You can use a function which is returning a valid element within array.

import numpy as np

superhero = np.array(["Ironman", "Thor", "Hulk"])
superhero[1] = np.array(["Vision"])

To replace or insert a value at an index in numpy array, you can use a single dimensional, single value array.

These examples will throw error –

import numpy as np

print(np.array(["Ironman", ("Thor", "Hulk")]))

The problem here is that numpy is unable to convert a tuple, ("Thor", "Hulk"), into array element.

import numpy as np

print(np.mean([10, tuple(range(5))]))

Same problem here too. We are trying to find the mean of array elements where one element is a tuple.

import numpy as np

def superhero():
    return ["Thor", "Hulk"]

print(np.array(["Ironman", superhero()]))

Here, the function superhero() is returning an array which is not a valid element for numpy array.

import numpy as np

superhero = np.array(["Ironman", "Thor", "Hulk"])
superhero[1] = np.array(["Vision", "Wanda"])

When you are replacing or inserting an element in numpy array at an index, you can have only single dimensional single value array. Here we are using single dimensional but multi value array which is a problem.

    Tweet this to help others

Live Demo

Open Live Demo