How to Solve “unhashable type: list” Error in Python

Hello geeks, and welcome in this article, we will be covering “unhashable type: list.” It is a type of error that we come across when writing code in python. In this article, ur main objective is to look at this error. Along with that, we will also try to troubleshoot and get rid of this error. We will achieve all this with a couple of examples. But first, let us try to get a brief overview of why this error occurs.

Python dictionaries only accept hashable data-types as a key. Here the hashable data-types means those values whose value remains the same during the lifetime. But when we use the list data-type, which is non-hashable, we get this kind of error.

The error-“unhahasble type: list”

In this section, we will look at the reason due to which this error occurs. We will take into account everything discussed so far. Let us see this through an example:

numb ={ 1:'one', [2,10]:'two and ten',11:'eleven'}
print(numb)

Output:

TypeError: unhashable type: 'list'

Here above, we have considered a straightforward example. Here we have used a dictionary to create a number dictionary and then tried to print it. But instead of the output, we get an error because we have used a list type in it. In the next section, we will see how to eliminate the error.

But Before that let us also look at an another example.

country=[
    {
    "name":"USA",[50,4]:"states and area",
    "name":"France",[27,48]:"states and area"}
]
print(country)
TypeError: unhashable type: 'list'

Here in the above example, we can see we come across the same problem. Here in this dictionary, we have taken the number of states and their ranking worldwide as the data. Now let’s quickly jump to the next section and eliminate these errors.

Troubleshooting:”unhashable type:list”

In this section, we will try to get rid of the errors. Let us start with the first example here. To rectify it all, we have to do is use a tuple.

numb ={ 1:'one', tuple([2,10]):'two and ten',11:'eleven'}
print(numb)
{1: 'one', (2, 10): 'two and ten', 11: 'eleven'}

With just a slight code change, we can rectify this. Here we have used a tuple, which is a hashable data data-type. Similarly, we can rectify the second error of the second example.

country=[
    {
    "name":"USA",tuple([50,4]):"states and area",
    "name":"France",tuple([27,48]):"states and area"}
]
print(country)
[{'name': 'France', (50, 4): 'states and area', (27, 48): 'states and area'}]

Again with the help of tuple we are able to rectify it. It is a simple error and can be rectified easily.

Difference between hashable and unhashable type

In this section, we see the basic difference between the 2 types. Also, we classify the various data-types that we use while coding in python under these 2 types.

HashableUnhashable
For this data-type, the value remains constant throughout.For this data-type, the value is not constant and change.
Some data types that fall under this category are- int, float, tuple, bool, string, bytes.Some data types that fall under this category are- list, set, dict, bytearray.

CONCLUSION

In this article, we covered unhashable type: list error. We looked at why it occurs and the methods by which we can rectify it. To achieve it, we looked at a couple of examples. In the end, we can conclude that this error arises when we use an unhashable data-type in a dictionary.

I hope this article was able to clear all doubts. But in case you have any unsolved queries feel free to write them below in the comment section. Done reading this why not read about the cPickle module.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments