Values in a Python dictionary cannot be sliced like a list. This is because dictionaries can have custom key values. They are not indexed from zero. If you try to slice a dictionary as if it were a list, you’ll encounter the “TypeError: unhashable type: ‘slice’” error.
This guide discusses what this error means and why you see it in your code. It discusses an example of this error to help you solve it.
TypeError: unhashable type: ‘slice’
A slice is a subset of a sequence such as a string, a list, or a tuple. The name gives away the purpose of a slice: it is “a slice” of a sequence.
Consider the following program:
news_sites = ["New York Times", "Washington Post", "CNN"] print(news_sites[:2])
This code retrieves the first two values in our “news_sites” list and prints them to the console. Our code returns: [‘New York Times’, ‘Washington Post’].
This is an example of slicing. You’re retrieving two objects from the list. By specifying a colon and an index value, you are telling Python which objects to retrieve.
Dictionaries cannot be sliced like a list. Dictionaries do not have any index numbers and so this syntax does not apply.
TypeError: unhashable type: ‘slice’
Build a program that displays information about a keyboard for sale at a computer hardware store. To start, define a dictionary with data about a keyboard:
keyboard = { "name": "Huntsman Mini", "brand": "Razer", "price": 119.99, "switch_type": "Razer Switches", }
The program stores information about the name of a keyboard, its price, the brand of the keyboard, and the switch type used by the keyboard. You only want to show: the name of a keyboard, the brand of the keyboard, and its price.
To do this, use slicing to retrieve the first three items in our dictionary. These items are the name of the keyboard, the brand, and the price:
show_to_customer = keyboard[:3]
This code retrieves the first three items in the dictionary. Next, use a for loop to iterate over this list and print each item to the console:
for s in show_to_customer: print(s[1])
You use indexing to retrieve the value from each record in the “show_to_customer” variable. You then print that value to the console using a print()
statement.
Let’s run the code and see what happens:
Traceback (most recent call last): File "main.py", line 8, in <module> show_to_customer = keyboard[:3] TypeError: unhashable type: 'slice'
Our code returns an error.
The Solution
Unlike lists, dictionaries cannot be sliced. You cannot retrieve any items in the dictionary using slicing because dictionaries do not have index numbers. Data is stored in key-value pairs. Because dictionaries cannot be sliced, the for
loop from earlier is not appropriate.
You must directly specify what values you want to access from our dictionary. To do this, refer to the appropriate key names in the dictionary.
To solve the code, let’s individually access each value you want to display on the console:
keyboard = { "name": "Huntsman Mini", "brand": "Razer", "price": 119.99, "switch_type": "Razer Switches", } print("Name: " + keyboard["name"]) print("Brand: " + keyboard["brand"]) print("Price: $" + str(keyboard["price"]))
Each print() statement refers to a different value from the dictionary. The first print statement prints the label “Name: ”, followed by the value of “name” in the dictionary, to the console. The second and third statements print the value of “brand” and “price” to the console, respectively.
You convert the “price” value to a string using the str()
method to concatenate it with the “Price: $” label using the concatenation operator (+).
Let’s run our new program:
Name: Huntsman Mini Brand: Razer Price: $119.99
The code successfully prints out the three pieces of information you wanted to display to the console. The user can see the name, brand, and price of a keyboard.
Conclusion
The “TypeError: unhashable type: ‘slice’” error is raised when you try to access items from a dictionary using slicing syntax. To solve this error, make sure you refer to the items you want to access from a dictionary directly.
Now you have the knowledge you need to solve this error like an expert!
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.