Handle Unhashable Type List Exceptions in Python
Last Updated :
28 Apr, 2025
Python, with its versatile and dynamic nature, empowers developers to create expressive and efficient code. However, in the course of Python programming, encountering errors is inevitable. One such common challenge is the "Unhashable Type List Exception". In this article, we will dive deep into the intricacies of Unhashable Type List Exception in Python. We will explore why this exception occurs and How to effectively handle them.
What is Unhashable Type List Exceptions In Python?
In Python programming, a Hashable object is something which is having a hash value. Hashable objects are immutable and cannot be modified after initialization. Similarly, unhashable objects do not have hash values such as list, dict, set, etc.
Why do Unhashable Type List Exceptions In Python occur?
Below are some of the reasons why Unhashable Type List Exception occurs in Python:
Initialize List as Key in Dictionary
In this example, we are using a list as a key in a dictionary that leads to an Unhashable Type List Exception in Python.
Python3
# initializing the list
l = ['a']
# initializing dictionary with list as the key
dictionary = {l: 1}
print(dictionary)
Output:
Error: Traceback (most recent call last):
File "<string>", line 3, in <module>
TypeError: unhashable type: 'list'
Initialize List as Set
In this example, we are adding a list inside a set that is not possible and leads to Unhashable Type List Exception in Python.
Python3
# initializing the list
l = ['a']
# initializing set
s = set()
# adding list as the element of the set
s.add(l)
print(s)
Output:
Error: Traceback (most recent call last):
File "<string>", line 8, in <module>
TypeError: unhashable type: 'list'
Handling Unhashable Type List Exceptions In Python
Below are some of the ways to handle Unhashable Type List Exceptions in Python:
- Convert List to Tuple
- Convert List to JSON Strings
Convert List to Tuple
We can convert the list to the corresponding tuple value and can further use it as the key for the dictionary or the element for the set.
Python
# initializing the list
l = ['a']
# initializing dictionary with list as the key
dictionary = {tuple(l):1}
print(dictionary)
# initializing the set
s = {tuple(l)}
print(s)
Output{('a',): 1}
set([('a',)])
Convert List to JSON Strings
The idea is to convert the list to the JSON and can further use it as the key for the dictionary or the element for the set.
Python
import json
# initializing the list
l = ['a']
# initializing dictionary with list as the key
dictionary = {json.dumps(l):1}
print(dictionary)
# initializing the set
s = {json.dumps(l)}
print(s)
Output{'["a"]': 1}
set(['["a"]'])
Similar Reads
TypeError: Unhashable Type 'Dict' Exception in Python In Python, the "Type Error" is an exception and is raised when an object's data type is incorrect during an operation. In this article, we will see how we can handle TypeError: Unhashable Type 'Dict' Exception in Python. What is TypeError: Unhashable Type 'Dict' Exception in PythonIn Python, certain
3 min read
Unique Dictionary Filter in List - Python We are given a dictionary in list we need to find unique dictionary. For example, a = [ {"a": 1, "b": 2}, {"a": 1, "b": 2}, {"c": 3}, {"a": 1, "b": 3}] so that output should be [{'a': 1, 'b': 2}, {'a': 1, 'b': 3}, {'c': 3}].Using set with frozensetUsing set with frozenset, we convert dictionary item
3 min read
Output of Python programs | Set 10 (Exception Handling) Pre-requisite: Exception Handling in PythonNote: All the programs run on python version 3 and above. 1) What is the output of the following program?Python data = 50 try: data = data/0 except ZeroDivisionError: print('Cannot divide by 0 ', end = '') else: print('Division successful ', end = '') try:
3 min read
Python | Type conversion in dictionary values The problem of conventional type conversion is quite common and can be easily done using the built-in converters of python libraries. But sometimes, we may require the same functionality in a more complex scenario vis. for keys of list of dictionaries. Let's discuss certain ways in which this can be
4 min read
Difference between List VS Set VS Tuple in Python In Python, Lists, Sets and Tuples store collections but differ in behavior. Lists are ordered, mutable and allow duplicates, suitable for dynamic data. Sets are unordered, mutable and unique, while Tuples are ordered, immutable and allow duplicates, ideal for fixed data. List in PythonA List is a co
2 min read
Python | Get unique tuples from list Sometimes, while working with Python list, we can come across a problem in which we require to find the unique occurrences of list. Having elementary data types is easy to handle, but sometime, we might have complex data types and the problem becomes new in that cases. Let's discuss certain ways in
3 min read