November 23, 2024
blog Tech

“TypeError: unhashable type”

“TypeError: unhashable type”

Error messages can be frustrating and confusing. For example, you might get a “TypeError: 

unhashable type” error message when you try to import a Python module. It’s not always clear 

how to fix these errors, especially given the variety of ways that an error like this can happen. 

This article will help you troubleshoot and solve the problem!

I’ll start by explaining what we mean by “unhashable.” Then we’ll take a look at why “TypeError: 

unhashable type” might pop up when importing modules in Python. Next, I’ll give some tips on 

how to find and fix those “TypeError: unhashable type” errors. We’ll also take a look at the interactive debugging features in the Idle IDE.

What’s Unhashable?

The word “unhashable” comes from computer science terminology. It refers to something that 

cannot be used as a key in a hash table. You might have used hash tables to find or store 

something with a unique identifier, like your cell phone number, an ISBN number, or some other 

reference number. A hash table uses one of these keys to find and retrieve its value (that is, whatever you stored).

Key differences between hash tables and Python dictionaries

One key difference between a dictionary and a hash table is that a dictionary can’t hold only one 

value. A dictionary can hold multiple values, which is useful for something like your friends’ 

phone numbers. But if you want to make sure that the same person’s phone number will always 

be returned, you don’t want to store this multiple-value information in a dictionary. That way, if 

your friend changes his or her number, it won’t be reflected in the phone numbers stored in your 

friends’ phones! Rather than using multiple values to represent an identifier, you could use one 

big integer . (This is what’s called a “key” and is used to look up the value of something like your 

phone number.) A hash table works pretty much the same way. Instead of using multiple values 

to represent an identifier, you use one value that nobody else uses, and then store that value in 

a hash table as a key. For example, it’s possible for some user to assign different identifiers 

(keys) to each of his or her friends’ phones so that every time anyone calls, the phone number on the caller ID is their friend’s phone number.

Here’s an analogy. Remember when you were in school? You might have gone to a school with 

iffy computer security. You probably remember the “secret codes” that you needed to use to 

access the computers. To login, for example, you needed to enter your computer access code. 

And in order to download files, you needed to enter the download access code so that only 

authorized people could use computers or download files undetected by their teachers or fellow students.

Imagine that one time, when I went into the lab at school, my teacher told me not to touch any of 

the computers because they were being fixed by tech support. He then proceeded to unlock the door and turn the key into the lock! I couldn’t believe it.

The next day, I went into the lab and my teacher pointed out that everything was still unlocked. 

Because of his careless mistake, any student could use any computer or download any files 

without entering a code. This is like what we mean by “unhashable” — you can’t use something as a key and expect to retrieve what you stored with that key.

Python Hashable and Unhashable Types

You can’t use a type as a key in a hash table. But you can use the same type in two different 

ways: You can find it , like when you run: print ( 1 ) and see that value’s value. Or you can 

change it, which is what happens when you run: 1 = 2 . When you change the value of a 

variable or an object, that’s sometimes called assigning or reassigning the variable. In Python, this is called “unhashing” the object.

But why would you want to “unhash” something? For example, imagine that you have an object (usually called obj ) like this: unhashable type list

obj = [] # creates an empty list

And suppose that you want to make obj into a list of integers. You could do this:

obj = [] # creates an empty list 1 , 2 , 3 for i in obj : print ( i ) # prints 1, 2, and 3 assigned to our 

‘obj’ variable. But if we wanted to print out the numbers that 1 , 2 , 3 represent, we need to use the “unhashable” type. You can do this like this:

x = 1 , 2 , 3 # x will contain the list of integers. Now, if we want to iterate through the values in 

Avatar for Aaron Finch

Aaron Finch

There are many labels that could be given to describe me, but one thing’s for certain: I am an entrepreneur with passion. Whether it's building websites and social media campaigns for new businesses or traveling the world on business trips - being entrepreneurs means constantly looking at yourself in a different light so as not get bored of your own success!

Leave a Reply

Your email address will not be published. Required fields are marked *