Movatterモバイル変換


[0]ホーム

URL:


Python Tutorial
  1. Python Introduction
  2. Python Fundamentals
  3. Python Control Flow
  4. Python Data Structures
  5. Python Files
  6. Python Essentials
  7. Python Exercises
  8. Python Networking
  9. Python Programming
  10. Python Exceptions
  11. Interview Questions

TypeError: unhashable type: 'list'

By:Rajesh P.S.

The error message "TypeError:unhashable type: 'list' " typically indicates an attempt to employ a list as a hashable argument. Hashing an unhashable object leads to an error. For example, using a list as a dictionary key is infeasible since lists aren't hashable. The conventional resolution involves converting the list to a tuple.

example
my_dict = {'name': 'John', [1,2,3]:'values'}print(my_dict)
outputWhat is Python: TypeError: unhashable type: 'list'

This error shows that the my_dict key [1,2,3] is List and List is not a hashable type inPython . 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 yourdictionary .

my_dict = {'name': 'John', tuple([1,2,3]):'values'}print(my_dict)
output
{'name': 'John', (1, 2, 3): 'values'}

The hash() function is a built-in Python method utilized to generate a distinct numerical value. It can be employed with user-defined objects that remain unaltered after initialization. This characteristic finds significance primarily in the context of 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

While tuples might appear similar to lists, they frequently serve distinct roles and have different applications. Tuples are immutable and typically comprise a heterogeneous sequence of elements accessible through unpacking or indexing. In contrast, lists are mutable and generally consist of homogeneous elements accessed by iteration.

Python Tuple and List

Hashing

Hashing is a fundamental concept in computer science that facilitates the creation of efficient, pseudo-random access data structures for rapid storage and retrieval of substantial data. Immutable objects, those that remain unmodifiable, are deemed hashable, possessing an unchanging unique value. A hashing function takes an object, such as the string "Java," and produces a consistent, fixed-size code—typically an integer.

Conclusion

The "TypeError: unhashable type: 'list'" error occurs when attempting to use a list as a hashable object. Lists are mutable and lack the properties necessary for reliable hashing, making them unsuitable asdictionary keys or elements in sets. To resolve this, consider using tuples, which are immutable and can serve as hashable alternatives in such contexts.




Next >  How to convert bytes to string in Python?


  1. Python Interview Questions (Part 2)
  2. Python Interview Questions (Part 3)
  3. What is python used for?
  4. Is Python interpreted, or compiled, or both?
  1. Explain how python is interpreted
  2. How do I install pip on Windows?
  3. How do you protect Python source code?
  4. What are the disadvantages of the Python?
  5. How would you achieve web scraping in Python?
  6. How to Python Script executable on Unix
  7. What is the difference between .py and .pyc files?
  8. What is __init__.py used for in Python?
  9. What does __name__=='__main__' in Python mean?
  10. What is docstring in Python?
  11. What is the difference between runtime and compile time?
  12. How to use *args and **kwargs in Python
  13. Purpose of "/" and "//" operator in python?
  14. What is the purpose pass statement in python?
  15. Why isn't there a switch or case statement in Python?
  16. How does the ternary operator work in Python?
  17. What is the purpose of "self" in Python
  18. How do you debug a program in Python?
  19. What are literals in python?
  20. Is Python call-by-value or call-by-reference?
  21. What is the process of compilation and Loading in python?
  22. Global and Local Variables in Python
  23. Static analysis tools in Python
  24. What does the 'yield' keyword do in Python?
  25. Python Not Equal Operator (!=)
  26. What is the difference between 'is' and '==' in python
  27. What is the difference between = and == in Python?
  28. How are the functions help() and dir() different?
  29. What is the python keyword "with" used for?
  30. Why isn't all memory freed when CPython exits
  31. Difference between Mutable and Immutable in Python
  32. Python Split Regex: How to use re.split() function?
  33. Accessor and Mutator methods in Python
  34. How to Implement an 'enum' in Python
  35. What is Object in Python?
  36. How to determine the type of instance and inheritance in Python
  37. Python Inheritance
  38. How is Inheritance and Overriding methods are related?
  39. How can you create a copy of an object in Python?
  40. Class Attributes vs Instance Attributes in Python
  41. Static class variables in Python
  42. Difference between @staticmethod and @classmethod in Python
  43. How to Get a List of Class Attributes in Python
  44. Does Python supports interfaces like in Java or C#?
  45. How To Work with Unicode strings in Python
  46. Difference between lists and tuples in Python?
  47. What are differences between List and Dictionary in Python
  48. Different file processing modes supported by Python
  49. Python append to a file
  50. Difference Between Multithreading vs Multiprocessing in Python
  51. Is there any way to kill a Thread in Python?
  52. What is the use of lambda in Python?
  53. What is map, filter and reduce in python?
  54. Is monkey patching considered good programming practice?
  55. What is "typeerror: 'module' object is not callable"
  56. How to convert bytes to string in Python?
  57. What are metaclasses in Python?





[8]ページ先頭

©2009-2025 Movatter.jp