All the Python objects like strings, integers, lists, dictionaries, and sets have a hashable property associated with them. Some of the objects are hashable, while others are not.
When we say a Python object is hashable, it means the value of that object will remain the same till the end of its life. For example, int, float, and strings.
Python objects like lists, dictionaries, sets, and byte arrays are unhashable. Meaning, the value of these objects might change. For example, we can remove or add an element to these objects. Hence, the value might change.
Understanding the root cause of TypeError: unhashable type: ‘numpy.ndarray’:
Programmatically, we can check if an object is hashable or not by using the hash()
function. If hash()
returns a number, it indicates that the object is hashable.
Let’s say we have a string. Let’s see what happens when we run the hash
function on the string object.
s="Finxter" print(hash(s))
Output:
951412520483326359
When we run the hash()
function on a string object, a number is returned, indicating it is a hashable object.
Now, let’s see what happens when we run the hash function on an ndarray
object.
arr=np.array([1,2,3,4]) print(hash(arr))
Output:
Traceback (most recent call last): File "C:\Users\...\unhashable_type_ndarray.py", line 18, in <module> print(hash(arr)) TypeError: unhashable type: 'numpy.ndarray'
We see an error, as the ndarray
object is not hashable.
I know what you’re thinking!
There is no hash()
function in my program. Why am I seeing this error?
That’s because you’re using a dictionary or set functions in the program. The dictionary keys and set elements must be hashable. So internally, these objects use hash()
to verify if the element you’re trying to add is hashable or not. For more details, check the source code for sets and dictionaries.
We see TypeError: unhashable type: 'numpy.ndarray'
, in the following cases:
- When we convert a multi-dimensional
ndarray
object to a set object. - When we assign a
ndarray
object as a dictionary key. - When we add a
ndarray
object to a set.
Now, let’s discuss each of these cases with an example.
Case 1: Converting a multi-dimensional ndarray object to a set object.
In Python, an iterable object can be converted to a set object using the set()
function.
Note that the elements of the set should be mandatorily hashable. When we call the set()
function on an array, the Python interpreter checks if the elements of the array are of the hashable type. If so, the elements of the ndarray object are converted to a set object.
To understand this better, let’s look at an example.
import numpy as np arr=np.array([1,2,3,4]) print(set(arr))
Output:
{1, 2, 3, 4}
In the above example, we have an array [1,2,3,4]
. The elements of this array are of integer type. Since int
is a hashable type, that array is successfully converted to a set object.
Now, let’s see what happens when we convert a multi-dimensional array.
import numpy as np arr=np.array([[1,2,3,4]]) print(set(arr))
Output:
Traceback (most recent call last): File "C:\Users\paian\PycharmProjects\Finxter\venv\share\unhashable_type_ndarray.py", line 4, in <module> print(set(arr)) TypeError: unhashable type: 'numpy.ndarray'
In this case, we see an error. Because the Python interpreter checks if elements of the array are hashable, it notices that the element is a ndarray
object. An error shows up as ndarray objects are not hashable.
Fix:
To fix this error, we have to access the element data correctly. In this case, we can solve the error by specifying set(arr[0])
.
This is shown in the following code snippet.
import numpy as np arr=np.array([[1,2,3,4]]) print(set(arr[0]))
Output:
{1, 2, 3, 4}
Case 2: Assigning a ndarray object as a dictionary key
In Python, only hashable objects can be added as a dictionary key. You will see an error if you add any unhashable object as a dictionary key.
Consider the following example:
import numpy as np arr=np.array([[1],[2],[3],[4]]) a=dict() # Adding the first element from the array as a dictionary key a[arr[0]]= "Value"
Output:
Traceback (most recent call last): File "C:\Users\paian\PycharmProjects\Finxter\venv\share\unhashable_type_ndarray.py", line 5, in <module> a[arr[0]]= "Value" TypeError: unhashable type: 'numpy.ndarray'
We see an error because arr[0]
is [1]
, which is a ndarray object (unhashable type)
Fix:
To fix this, index the inner element rightly as shown below.
import numpy as np arr=np.array([[1],[2],[3],[4]]) a=dict() # Adding the first element from the array as a dictionary key a[arr[0,0]]= "Value" print(a)
Output:
{1: 'Value'}
Case 3: Adding a ndarray object to a set.
Set objects can contain only hashable elements. You will see an error if you add any unhashable object types as a set element.
Example:
Let’s see what happens if there is an array [1,2,3,4]
and you want to add all the elements of this array to a set.
import numpy as np arr=np.array([1,2,3,4]) a=set() a.add(arr)
Output:
Traceback (most recent call last): File "C:\Users\...\unhashable_type_ndarray.py", line 5, in <module> a.add(arr) TypeError: unhashable type: 'numpy.ndarray'
We see an error as we add the array object instead of its elements in the set.add()
function.
Fix:
To fix this, add the elements of the array instead of the array object, as shown below:
import numpy as np arr=np.array([1,2,3,4]) a=set() for ele in arr: a.add(ele) print(a)
Output:
{1, 2, 3, 4}
Conclusion
We’ve reached the end of this article. I hope this has been informative. Kindly comment and let us know if you were able to fix this issue. Stay tuned to us and subscribe to our email newsletter for more interesting content.
Programmer Humor
![](https://blog.finxter.com/wp-content/uploads/2022/06/image-163.png)