Are you getting the “TypeError: Unhashable Type: ‘Slice’” error while slicing a dictionary in Python?
In Python, we cannot slice the values of a dictionary because there aren’t any index values assigned to dictionaries like lists. Rather we custom key values, and they aren’t indexed from zero. Therefore when you try to slice dictionaries, you’ll encounter the Type Error: unhashable type: ‘slice’.
In this article, we’ll discuss what is the TypeError unhashable type slice in Python and how to fix it. So let’s begin 🙂.
Table of Contents
What is TypeError: Unhashable Type: ‘Slice’ in Python?
The TypeError unhashable type slice when you try to treat a dictionary like a list. We all know that dictionaries are unhashable; they have custom keys and don’t operate on an index basis. Whereas lists work on an index basis, so we can slice them.
Let’s try to slice a dictionary and a list and see what happens.
Code
# creating a list l = [1, 2, 3, 4, 5, 6, 7, 8] print(l) # slice the list from index 0 to 4, including index 4 print("List sliced from index 0 - 4: ",l[0:4]) # slice the list from index 4 to 8, including index 8 print("List sliced from index 4 - 8: ",l[4:8])
Output
[1, 2, 3, 4, 5, 6, 7, 8] List sliced from index 0 - 4: [1, 2, 3, 4] List sliced from index 4 - 8: [5, 6, 7, 8]
Slicing works perfectly with lists because they operate on an index basis. Now, let’s try slicing on dictionaries:
Code
# creating a dictionary d = { "One" : 1, "Two" : 2, "Three": 3, "Four" : 4, "Five" : 5 } print(d) # slicing a dictionary print(d[1:5]) # --> TypeError: unhashable type: 'slice'
Output
We cannot slice dictionaries in Python because they don’t operate on index bases. Instead, they work in a key-value structure. Each pair has a custom key with a specific value against it, and to access the values, you’ll need the corresponding key.
How to Fix the TypeError: Unhashable Type: ‘Slice’ in Python?
As we know that we cannot slice a dictionary, but there are ways to access each individual value of the dictionary.
Let’s see an example of how to access the individual values of a dictionary in Python.
Code
# creating a dictionary d = { "One" : 1, "Two" : 2, "Three": 3, "Four" : 4, "Five" : 5 } print(d) print(d["One"]) print(d["Two"])
Output
{'One': 1, 'Two': 2, 'Three': 3, 'Four': 4, 'Five': 5} 1 2
To access each individual element of a dictionary, we’ll need its corresponding key, as demonstrated in the above example.
We can also use a for loop to iterate over the key-value pairs of the dictionary with the help of the items() function; let’s see an example:
Code
# creating a dictionary d = { "One" : 1, "Two" : 2, "Three": 3, "Four" : 4, "Five" : 5 } print(d) for k,v in d.items(): print(k,"\t:",v)
Output
{'One': 1, 'Two': 2, 'Three': 3, 'Four': 4, 'Five': 5} One : 1 Two : 2 Three : 3 Four : 4 Five : 5
There is another way of around of doing the same task with the help of keys() function.
Code
# creating a dictionary d = { "One" : 1, "Two" : 2, "Three": 3, "Four" : 4, "Five" : 5 } print(d) for k in d.keys(): print(k,"\t:",d[k])
Output
{'One': 1, 'Two': 2, 'Three': 3, 'Four': 4, 'Five': 5} One : 1 Two : 2 Three : 3 Four : 4 Five : 5
The keys() give us the keys of that specific dictionary which we can use as an index to access the corresponding value.
Conclusion
To summarize this article on how to fix the TypeError unhashable type slice, we have discussed what the “TypeError is: unhashable type: ‘slice’” error in Python and how to resolve it. In addition, we have discussed how to work on a dictionary using the items() and keys() functions.
Let’s have a quick recap of the topics discussed in the articles.
- What is a Type Error unhashable type slice in Python?
- How to iterate a dictionary in Python?
- How to fix TypeError: unhashable type slice in Python?
It’s time to explore more 🤔; can we get the dictionary keys using the values in Python?