The TypeError: Unhashable Type: Slice error occurs when you try to slice the data structure that does not index from zero. For example, in the case of the Python dictionary, we cannot slice the as-we-do lists because the dictionary does not have indexing ( starting from 0) like lists. Instead, they have key-value pairs. In this short article, we will discuss the reasons for getting TypeError Unhashable Type: Slice error and will solve the error using various methods. Moreover, we will cover how we can interpret errors in Python.

How to Solve TypeError: Unhashable Type: ‘Slice’ in Python?
We know that the dictionaries in Python do not have indexing as lists or other sequential data structures have. They have key-value pairs and if we try to perform the slicing on the dictionary using the index value, we will get TypeError: Unhashable Type Slice error. Here are some of the possible reasons for getting the error and we will solve each one separately
- Because of slicing dictionary
- Because of slicing pandas data frame
- Because of slicing JSON file
- This can occur when working with Beautifull Soup
A slice is simply a subset of the sequence. For example, we can get a small part of a list, string, or tuple by using the slicing method. Let us see, how we can perform slicing on these data structures:
# list
list1 = [1,2,3,4,5,6,7,8,9]
# tuple
tuple1 = (1,2,3,4,5,6,7,8,9)
# string
string = "Bashir Alam"
# slicing
print(list1[3:7])
print(tuple1[1:5])
print(string[7:])
Output:
[4, 5, 6, 7]
(2, 3, 4, 5)
Alam
As you can see, we were able to get the subset of the given data structures. But if we will try to apply the slicing on a dictionary, we will get an error as shown below:
# dictionary in Python
dictionary = {'first': 1, "second":2, "third": 3, "Fourth": 4}
# slicing dictionary
print(dictionary[2:])
Output:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
/tmp/ipykernel_25867/3575954621.py in <module>
3
4 # slicing dictionary
----> 5 print(dictionary[2:])
TypeError: unhashable type: 'slice'
As you can see, we got the error because we are not allowed to perform the slicing on a dictionary in Python.
Solution-1: Using itertools to slice the dictionary
One of the methods to get sliced off a dictionary in Python is to use the itertools which helps us to iterate through the dictionary.
import itertools
# dictionary in Python
dictionary = {'first': 1, "second":2, "third": 3, "Fourth": 4}
# slicing dictionary
new_dict = dict(itertools.islice(dictionary.items(), 2))
print(new_dict)
Output:
{'first': 1, 'second': 2}
As you can see, we first imported the module and then sliced the dictionary using it. If you got the TypeError: Unhashable Type because of slicing the dictionary, then you can use this method.
You can also use the itertools to get the range of elements from the dictionary as shown below:
import itertools
# dictionary in Python
dictionary = {'first': 1, "second":2, "third": 3, "Fourth": 4}
# slicing dictionary
new_dict = dict(itertools.islice(dictionary.items(), 2, 5))
print(new_dict)
Output:
Output:
Notice that this time, we got the last two elements of the dictionary using the itertools.
Solution-2: Using the For Loop in Python
We can also use the for loop in order to slice a dictionary in Python as shown below:
# dictionary in Python
dictionary = {'first': 1, "second":2, "third": 3, "Fourth": 4}
keys = ("first", "third")
# slicing dictionary
sliced = { i : dictionary[i] for i in keys}
print(sliced)
Output:
{'first': 1, 'third': 3}
As you can see, we were able to iterate through the specified keys and print the sliced dictionary.
Solution-3: Error in Pandas Data Frame
Sometimes, we got TypeError: Unhashable Type Slice error when dealing with Pandas data frames. The following code is an incorrect way to slice the pandas data frame.
# slicing pandas data frame
data[10: 30, : ]
# finding the shape of the data frame
data[10:30, :].shape
This will raise the error, the correct way is given below:
# slicing pandas data frame
data.iloc[10: 30, : ]
# finding the shape of the data frame
data.iloc[10:30, :].shape
As you can see, we have used the iloc[] method in order to slice a data frame which will help us to get rid of the error.
Solution-4: Unhashable error in JSON file
This error also occurs when you iterate over a JSON file incorrectly. If you are using the for loop in the following way then it causes this error:
for i in data['myItem'][:3]:
res = i['attribute']
print(res)
The correct way of iterating over a JSON file is given below:
for i in data(['myItem'].items())[:3]:
res = i['attribute']
print(res)
This will help you to get rid of TypeError: Unhashable Type: ‘Slice error.
Understanding TypeError: Unhashable Type: ‘Slice’ error
As you can see, the error has two main parts. The first part of the error shows the category of the error. In this case, the category of the error is TypeError which means there is a problem with the operation and the datatype but we don’t know what it is. The second part of the error helps us to figure out what kind of TypeError is there and it gives more specific details. For example, in this case, it says unhashable type slice which means we are performing the slicing operation on a data structure that does not support it.
Summary
The error occurs when we try to perform a slicing operation on a data structure that does not support the slicing. For example dictionaries, pandas data frames, and JSON files. In this short article, we discussed how we could solve the TypeError: Unhashable Type: ‘Slice’ error using various methods.