TypeError: unhashable type: 'list'
TypeError: unhashable type: 'list' usually means that you are trying to use a list as an hash argument. This means that when you try to hash an unhashable object it will result an error. For ex. when you use a list as a key in the dictionary , this cannot be done because lists can't be hashed. The standard way to solve this issue is to cast a list to a tuple .
my_dict = {'name': 'John', [1,2,3]:'values'}
print(my_dict)
output
Traceback (most recent call last):
File "sample.py", line 3, in <module>
my_dict = {'name': 'John', [1,2,3]:'values'}
TypeError: unhashable type: 'list'
This error shows that the my_dict key [1,2,3] is List and List is not a hashable type in Python . Dictionary keys must be immutable types and list is a mutable type.
Fix: Cast list to a tuple
You'll have to change your list into tuples if you want to put them as keys in your dictionary .
my_dict = {'name': 'John', tuple([1,2,3]):'values'}
print(my_dict)
output
{'name': 'John', (1, 2, 3): 'values'}
The hash() is a built-in python method, used to return a unique number . This can be applied to any user-defined object which won’t get changed once initialized. This property is used mainly in dictionary keys .
Examples of hashable objects:
int, float, decimal, complex, bool, string, tuple, range, frozenset, bytes
Examples of Unhashable objects:
list, dict, set, bytearray, user-defined classes
Tuple and List
Though tuples may seem similar to lists, they are often used in different situations and for different purposes. Tuples are immutable , and usually contain an heterogeneous sequence of elements that are accessed via unpacking or indexing . Lists are mutable, and their elements are usually homogeneous and are accessed by iterating over the list.
Hashing
Hashing is a concept in computer science which is used to create high performance , pseudo random access data structures where large amount of data is to be stored and accessed quickly. Immutable objects , or objects that can't be altered, are hashable and they have a single unique value that will never change. A hashing function is a function which takes an object, say a string such as "Java" and returns a fixed-size code, assume the return value is an integer .
Related Topics
- Python Interview Questions (Part 2)
- Python Interview Questions (Part 3)
- What is python used for?
- Is Python interpreted, or compiled, or both?
- Explain how python is interpreted
- How do I install pip on Windows?
- How do you protect Python source code?
- What are the disadvantages of the Python?
- How would you achieve web scraping in Python?
- How to Python Script executable on Unix
- What is the difference between .py and .pyc files?
- What is __init__.py used for in Python?
- What does __name__=='__main__' in Python mean?
- What is docstring in Python?
- What is the difference between runtime and compile time?
- How to use *args and **kwargs in Python
- Purpose of "/" and "//" operator in python?
- What is the purpose pass statement in python?
- Why isn't there a switch or case statement in Python?
- How does the ternary operator work in Python?
- What is the purpose of "self" in Python
- How do you debug a program in Python?
- What are literals in python?
- What is Python's parameter passing mechanism?
- What is the process of compilation and Loading in python?
- Global and Local Variables in Python
- Is there a tool to help find bugs or perform static analysis?
- What does the 'yield' keyword do in Python?
- Comparison Operators != is not equal to in Python
- What is the difference between 'is' and '==' in python
- What is the difference between = and == in Python?
- How are the functions help() and dir() different?
- What is the python keyword "with" used for?
- Is all the memory freed when Python exits?
- Difference between Mutable and Immutable in Python
- Explain split() methods of "re" module in Python
- Accessor and Mutator methods in Python
- How to Implement an 'enum' in Python
- Important characteristics of Python Objects
- How to determine the type of instance and inheritance in Python
- How would you implement inheritance in Python?
- How is Inheritance and Overriding methods are related?
- How can you create a copy of an object in Python?
- How to avoid having class data shared among instances in Python?
- Static class variables in Python
- Difference between @staticmethod and @classmethod in Python
- How to Get a List of Class Attributes in Python
- Does Python supports interfaces like in Java or C#?
- What is used to create Unicode string in Python?
- Difference between lists and tuples in Python?
- What are differences between List and Dictionary in Python
- Different file processing modes supported by Python
- How do you append to a file in Python?
- What are the differences between the threading and multiprocessing?
- Is there any way to kill a Thread in Python?
- What is the use of lambda in Python?
- What is map, filter and reduce in python?
- Is monkey patching considered good programming practice?
- What is "typeerror: 'module' object is not callable"
- How to convert bytes to string in Python?
- What are metaclasses in Python?