Last modified: Apr 28, 2026 By Alexander Williams

Fix TypeError: Unhashable Type NumPy Array

Encountering a TypeError: unhashable type 'numpy.ndarray' can be frustrating. This error is common when working with NumPy arrays in Python. It usually happens when you try to use an array as a dictionary key or add it to a set.

This guide explains why the error occurs. It also provides simple fixes with code examples. By the end, you will understand how to avoid this issue in your projects.

Why Does This Error Happen?

Python dictionaries and sets require hashable keys. Hashable objects have a fixed hash value. This allows Python to quickly look up items.

NumPy arrays are mutable. Their contents can change. Mutable objects cannot be hashable. When you try to use a NumPy array as a key, Python raises the TypeError.

Consider this example:

 
import numpy as np

# Create a simple array
arr = np.array([1, 2, 3])

# Try to use it as a dictionary key
my_dict = {arr: "value"}

Running this code will produce:


TypeError: unhashable type: 'numpy.ndarray'

The error tells you exactly which type is unhashable. In this case, it is numpy.ndarray.

Common Scenarios That Trigger the Error

The error often appears in these situations:

  • Using an array as a dictionary key.
  • Adding an array to a set.
  • Using an array inside another data structure like a list of sets.

Here is an example with a set:

 
import numpy as np

arr = np.array([4, 5, 6])
my_set = {arr}  # This will fail

The error is the same. Sets require hashable elements.

How to Fix the Error

There are several ways to fix this error. The best method depends on your use case.

1. Convert the Array to a Tuple

Tuples are immutable and hashable. You can convert your array to a tuple before using it as a key.

 
import numpy as np

arr = np.array([1, 2, 3])
key = tuple(arr)  # Convert to tuple

my_dict = {key: "value"}
print(my_dict)

Output:


{(1, 2, 3): 'value'}

This works because the tuple is hashable. Note that this creates a copy of the data.

2. Use the Array as a String

You can also convert the array to a string representation. This is useful for debugging or logging.

 
import numpy as np

arr = np.array([7, 8, 9])
key = str(arr)

my_dict = {key: "example"}
print(my_dict)

Output:


{'[7 8 9]': 'example'}

Be careful. The string representation may not be unique for all arrays.

3. Use the Array's Bytes or Hash

If you need a unique hash, you can use the tobytes() method. This creates a bytes object that is hashable.

 
import numpy as np

arr = np.array([10, 20, 30])
key = arr.tobytes()  # Convert to bytes

my_dict = {key: "data"}
print(my_dict)

Output:


{b'\n\x00\x00\x00\x14\x00\x00\x00\x1e\x00\x00\x00': 'data'}

This method is efficient but produces a long key.

4. Avoid Using Arrays as Keys

The simplest fix is to redesign your code. Use integers, strings, or tuples as keys instead of arrays.

For example, use an index or a label:

 
import numpy as np

arrays = [np.array([1,2]), np.array([3,4])]
# Use index as key instead of the array itself
my_dict = {0: arrays[0], 1: arrays[1]}
print(my_dict)

Output:


{0: array([1, 2]), 1: array([3, 4])}

This approach keeps your data structure clean.

Understanding Hashability in Python

Hashability is related to the __hash__ method. Immutable objects like integers, strings, and tuples implement this method. Mutable objects like lists and arrays do not.

When you create a dictionary, Python calls hash(key). If the object is not hashable, you get the error.

For more on Python data handling, check out our Python Character Encoding Guide for Beginners. It covers how Python handles different data types.

Using Arrays in Sets

Sets also require hashable elements. If you need to store arrays in a set-like structure, consider using a list or a custom class.

 
import numpy as np

# This will fail
arr1 = np.array([1, 2])
arr2 = np.array([1, 2])
my_set = {arr1, arr2}

Instead, store tuples in a set:

 
import numpy as np

arr1 = tuple(np.array([1, 2]))
arr2 = tuple(np.array([1, 2]))
my_set = {arr1, arr2}
print(my_set)

Output:


{(1, 2)}

This works because tuples are hashable.

Best Practices to Avoid the Error

Follow these tips to stay safe:

  • Always convert arrays to hashable types before using them as keys.
  • Use meaningful labels or indices instead of arrays.
  • Test your code with small examples to catch errors early.

If you are new to Python, our Python Character Encoding Guide for Beginners can help you understand data types better.

Conclusion

The TypeError: unhashable type 'numpy.ndarray' is a common mistake. It happens because NumPy arrays are mutable and cannot be hashed. The fix is simple: convert the array to a tuple, string, or bytes object. Alternatively, redesign your code to avoid using arrays as keys.

Always remember that dictionary keys and set elements must be immutable. By following the examples in this guide, you can solve this error quickly. Practice with small code snippets to build confidence.

Happy coding and see our Python Character Encoding Guide for Beginners for more Python tips.