<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>Coder Legion - Recent posts tagged hash</title>
<link>https://coderlegion.com/tag/hash</link>
<description>Powered by Question2Answer</description>
<item>
<title>Typeerror: 'series' objects are mutable, thus they cannot be hashed</title>
<link>https://coderlegion.com/26615/typeerror-series-objects-are-mutable-thus-they-cannot-be-hashed</link>
<description>&lt;p&gt;
&lt;strong&gt;&lt;span style=&quot;color: #ff04fc;&quot;&gt;TypeError: 'Series' objects are mutable, thus they cannot be hashed&lt;/span&gt; is an error message that can occur when working with pandas, a popular data manipulation library for Python. This error message is raised when trying to use a pandas Series object (a one-dimensional array-like object) as a key in a dictionary or as an element in a set. In this article we are going to look at the idea behind hashing and why a mutable object cannot be hashed which will give raise to the error in consideration.
&lt;/strong&gt;
&lt;/p&gt;&lt;hr&gt;
&lt;div id=&quot;toc-div&quot;&gt;
&lt;h3&gt;&lt;span style=&quot;font-family:arial,helvetica,sans-serif;&quot;&gt;&lt;strong&gt; Table of Contents:&lt;a id=&quot;toc-toogle&quot; class=&quot;open&quot;&gt;&lt;/a&gt;&lt;/strong&gt;&lt;/span&gt;&lt;/h3&gt;
&lt;div id=&quot;toc-list&quot;&gt;
&lt;ol&gt;
&lt;li&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;#brief-note-on-hashing&quot;&gt;&lt;span style=&quot;font-family:arial,helvetica,sans-serif;&quot;&gt;Brief note on hashing&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;#why-the-error&quot;&gt;&lt;span style=&quot;font-family:arial,helvetica,sans-serif;&quot;&gt;Why the error?&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;#a-walk-around&quot;&gt;&lt;span style=&quot;font-family:arial,helvetica,sans-serif;&quot;&gt;A walk around&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;#conclusion&quot;&gt;&lt;span style=&quot;font-family:arial,helvetica,sans-serif;&quot;&gt;Conclusion&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;#references&quot;&gt;&lt;span style=&quot;font-family:arial,helvetica,sans-serif;&quot;&gt;References&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;h3&gt;&lt;span style=&quot;font-family:arial,helvetica,sans-serif;&quot;&gt;Brief note on hashing&lt;/span&gt;&lt;a id=&quot;brief-note-on-hashing&quot; class=&quot;custom_anchor&quot;&gt;&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;In Python, a &lt;span style=&quot;color: #ff04fc;&quot;&gt;series&lt;/span&gt; object is a one-dimensional array-like object that can hold any data type. It is similar to a column in a spreadsheet or a dataframe in pandas. One of the key features of a &lt;span style=&quot;color: #ff04fc;&quot;&gt;series&lt;/span&gt; object is that it is mutable, meaning that its values can be changed. However, this also means that 'series' objects cannot be hashed.&lt;/p&gt;
&lt;p&gt;Hashing is a process of converting an input of any length into a fixed-length output, known as a hash value. The hash value is a unique representation of the input, and it is often used for efficient data searching and indexing. However, since 'series' objects are mutable, they cannot be hashed as their values can change, making it impossible to generate a unique hash value.&lt;/p&gt;
&lt;p&gt;One of the most popular uses of hashing is in data structures such as hash tables and hash maps. These data structures use a hash function to convert a key (such as a name or a product ID) into a unique index in an array. This allows for quick and efficient lookup of the corresponding value (such as a price or a product description).&lt;/p&gt;
&lt;div class=&quot;div-green&quot;&gt;
&lt;span class=&quot;alert-header&quot;&gt;Tip&lt;/span&gt;
&lt;span class=&quot;alert-body&quot;&gt;Hashing algorithms come in a variety of forms, such as &lt;span style=&quot;color: #ff04fc;&quot;&gt;MD5&lt;/span&gt;, &lt;span style=&quot;color: #ff04fc;&quot;&gt;SHA-1&lt;/span&gt;, and &lt;span style=&quot;color: #ff04fc;&quot;&gt;SHA-256&lt;/span&gt;, each with their own unique properties and trade-offs. For example, while MD5 is fast and widely supported, it is not considered as secure as the SHA-256 algorithm.&lt;/span&gt;
&lt;/div&gt;
&lt;h3&gt;&lt;span style=&quot;font-family:arial,helvetica,sans-serif;&quot;&gt;Why the error?&lt;/span&gt;&lt;a id=&quot;why-the-error&quot; class=&quot;custom_anchor&quot;&gt;&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;The reason for the error &lt;span style=&quot;color: #ff04fc;&quot;&gt;Typeerror: 'series' objects are mutable, thus they cannot be hashed&lt;/span&gt; is that in Python, dictionaries and sets use a technique called &lt;span style=&quot;color: #ff04fc;&quot;&gt;hashing&lt;/span&gt; to quickly look up values based on their keys. Hashing involves taking an input (in this case, the key of a dictionary or set), running it through a mathematical function, and producing a fixed-length output (the &quot;hash value&quot;). The hash value is then used to quickly locate the corresponding value in the dictionary or set.&lt;/p&gt;
&lt;p&gt;Here is an example of trying to hash a 'series' object:&lt;br&gt;
&lt;/p&gt;&lt;pre&gt;&lt;code&gt;&lt;span style=&quot;font-family:arial,helvetica,sans-serif;&quot;&gt;&lt;br&gt;
import pandas as pd
Creating a 'series' object
s = pd.Series([1, 2, 3])
Attempting to hash the 'series' object
hash(s)&lt;br&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The above code will result in the following error:&lt;br&gt;
&lt;/p&gt;&lt;pre&gt;&lt;code&gt;&lt;span style=&quot;font-family:arial,helvetica,sans-serif;&quot;&gt;&lt;br&gt;
TypeError: unhashable type: 'Series'&lt;br&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;However, for a data structure to be usable as a key, it must meet certain requirements. One of the most important of these is that the key must be immutable, meaning that its value cannot be changed after it is created. This is because the hash value of a key is calculated based on its value, so if the key's value changes, its hash value would also change. This would make it impossible to locate the key in the dictionary or set.&lt;/p&gt;
&lt;p&gt;Pandas Series objects are mutable, meaning that their values can be changed after they are created. This makes them incompatible with dictionaries and sets, as their hash value would change if the values in the Series were modified.&lt;/p&gt;
&lt;p&gt;However, if a 'series' object needs to be used in a hash table, it must first be converted to an immutable object such as a tuple or a frozen set.&lt;/p&gt;
&lt;h3&gt;&lt;span style=&quot;font-family:arial,helvetica,sans-serif;&quot;&gt;A walk around&lt;/span&gt;&lt;a id=&quot;a-walk-around&quot; class=&quot;custom_anchor&quot;&gt;&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;You can convert the Series to an immutable object such as a tuple or a string using the &lt;span style=&quot;color: #ff04fc;&quot;&gt;&lt;code&gt;tolist()&lt;/code&gt;&lt;/span&gt; method or use &lt;span style=&quot;color: #ff04fc;&quot;&gt;&lt;code&gt;pd.Index&lt;/code&gt;&lt;/span&gt; class to create an immutable index.&lt;/p&gt;
&lt;p&gt;Here is an example of converting a 'series' object to a tuple:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&lt;span style=&quot;font-family:arial,helvetica,sans-serif;&quot;&gt;
import pandas as pd

# Creating a 'series' object
s = pd.Series([1, 2, 3])

# Converting the 'series' object to a tuple
s = tuple(s)

# Hashing the tuple
print(hash(s))
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The above code will output a hash value for the tuple, allowing it to be used in a hash table.&lt;/p&gt;
&lt;div class=&quot;div-pink&quot;&gt;
&lt;span class=&quot;alert-header&quot;&gt;Note&lt;/span&gt;
&lt;span class=&quot;alert-body&quot;&gt;It is important to keep in mind that this conversion may affect the performance of the code, as the conversion process may take a longer time compared to using a mutable object.&lt;/span&gt;
&lt;/div&gt;
&lt;h3&gt;&lt;span style=&quot;font-family:arial,helvetica,sans-serif;&quot;&gt;Conclusion&lt;/span&gt;&lt;a id=&quot;conclusion&quot; class=&quot;custom_anchor&quot;&gt;&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;In conclusion, the &lt;span style=&quot;color: #ff04fc;&quot;&gt;TypeError: 'Series' objects are mutable, thus they cannot be hashed&lt;/span&gt; is raised when trying to use a pandas Series object as a key in a dictionary or as an element in a set because they are mutable objects. To fix this error, you can convert the Series to an immutable object such as a tuple or a string or use &lt;span style=&quot;color: #ff04fc;&quot;&gt;&lt;code&gt;pd.Index&lt;/code&gt;&lt;/span&gt; class to create an immutable index.&lt;/p&gt;
&lt;h3&gt;&lt;span style=&quot;font-family:arial,helvetica,sans-serif;&quot;&gt;References&lt;/span&gt;&lt;a id=&quot;references&quot; class=&quot;custom_anchor&quot;&gt;&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;Pandas documentation on Series: &lt;a rel=&quot;nofollow&quot; href=&quot;https://pandas.pydata.org/pandas-docs/stable/user_guide/dsintro.html#series&quot;&gt;https://pandas.pydata.org/pandas-docs/stable/user_guide/dsintro.html#series&lt;/a&gt;&lt;br&gt;
Python documentation on dictionaries: &lt;a rel=&quot;nofollow&quot; href=&quot;https://docs.python.org/3/tutorial/datastructures.html#dictionaries&quot;&gt;https://docs.python.org/3/tutorial/datastructures.html#dictionaries&lt;/a&gt;&lt;br&gt;
Python documentation on sets: &lt;a rel=&quot;nofollow&quot; href=&quot;https://docs.python.org/3/tutorial/datastructures.html#sets&quot;&gt;https://docs.python.org/3/tutorial/datastructures.html#sets&lt;/a&gt;&lt;br&gt;
Understanding Hash Tables: &lt;a rel=&quot;nofollow&quot; href=&quot;https://en.wikipedia.org/wiki/Hash_table&quot;&gt;https://en.wikipedia.org/wiki/Hash_table&lt;/a&gt;&lt;br&gt;
How to convert pandas Series to a NumPy array: &lt;a rel=&quot;nofollow&quot; href=&quot;https://datatofish.com/convert-pandas-dataframe-to-numpy-array/&quot;&gt;https://datatofish.com/convert-pandas-dataframe-to-numpy-array/&lt;/a&gt;&lt;/p&gt;
</description>
<category>Articles</category>
<guid isPermaLink="true">https://coderlegion.com/26615/typeerror-series-objects-are-mutable-thus-they-cannot-be-hashed</guid>
<pubDate>Wed, 09 Sep 2026 11:49:50 +0000</pubDate>
</item>
<item>
<title>Understanding and debugging - TypeError :unhashable type: dict</title>
<link>https://coderlegion.com/7686/understanding-and-debugging-typeerror-unhashable-type-dict</link>
<description>&lt;h2 id=&quot;introduction&quot;&gt;Introduction&lt;/h2&gt;
&lt;p&gt;Sometimes while executing a python code, we might run into an error like &lt;strong&gt;&lt;em&gt;TypeError: unhashable type: ‘dict’&lt;/em&gt;&lt;/strong&gt; . For example, if we try to hash a dictionary by trying to use it as a &lt;strong&gt;set element&lt;/strong&gt; or as a &lt;strong&gt;dictionary key&lt;/strong&gt;, we might get this error. A  &lt;strong&gt;&lt;em&gt;TypeError&lt;/em&gt;&lt;/strong&gt; is  raised when an operation or function is applied to an object of inappropriate type. To avoid this, we must ensure that a function is applied on the right type of object. In this article we will understand in detail why this error occurs, as well as ways to avoid it by using alternate datatypes. &lt;/p&gt;
&lt;hr&gt;
&lt;h2 id=&quot;why-this-error-occurs&quot;&gt;Why this error occurs&lt;/h2&gt;
&lt;p&gt;If we try to use an unhashable datatype in our code where hashing is required the following error will occur. &lt;strong&gt;TypeError: unhashable type: &lt;em&gt;name_of_datatype&lt;/em&gt;&lt;/strong&gt;. Before getting any further, let us first understand the meaning of  &lt;strong&gt;&lt;em&gt;mutable&lt;/em&gt;&lt;/strong&gt; and   &lt;a rel=&quot;nofollow&quot; href=&quot;https://docs.python.org/3/glossary.html#term-hashable&quot;&gt;hashable&lt;/a&gt; in the context of Python. Understanding these concepts is crucial to really deep dive into the error and completely understand it.&lt;/p&gt;
&lt;h3 id=&quot;mutable-and-immutable-datatypes-&quot;&gt;Mutable and Immutable datatypes:&lt;/h3&gt;
&lt;p&gt;&lt;strong&gt;Mutable&lt;/strong&gt;: In python, if we can change the value in an object without changing its identity, it is a mutable object. For example, if we create a list and try to change its individual elements, the operation is possible.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;my_list = [1,2]
my_list[1] = 3
my_list
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&quot;https://logiclair.org/?qa=blob&amp;amp;qa_blobid=5874861095621207936&quot; alt=&quot;&quot;&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Immutable&lt;/strong&gt;: In python, if we cannot change the value in an object without changing its identity, it is immutable.&lt;br&gt;
For example: Changing the value of individual elements in a tuple is not allowed and would raise an error.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;my_tuple = (1,2)
my_tuple[1] = 3
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&quot;https://logiclair.org/?qa=blob&amp;amp;qa_blobid=7182767810221039665&quot; alt=&quot;&quot;&gt;&lt;/p&gt;
&lt;h3 id=&quot;what-is-a-hashable-object-&quot;&gt;What is a Hashable Object:&lt;/h3&gt;
&lt;p&gt; An object is &lt;strong&gt;&lt;em&gt;hashable&lt;/em&gt;&lt;/strong&gt; if it has a &lt;strong&gt;&lt;em&gt;hash value&lt;/em&gt;&lt;/strong&gt; which never changes during its lifetime. Hashing allows for efficient lookup, insertion, and deletion of elements. Some objects in Python are hashed internally.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Hash value&lt;/strong&gt;: We can view the hash value of objects using the &lt;a rel=&quot;nofollow&quot; href=&quot;https://docs.python.org/3/library/functions.html#hash&quot;&gt;hash()&lt;/a&gt; function in Python. &lt;/p&gt;
&lt;pre&gt;&lt;code&gt;my_tuple = (1,2,3)
hash(my_tuple)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&quot;https://logiclair.org/?qa=blob&amp;amp;qa_blobid=3455545544590401939&quot; alt=&quot;&quot;&gt;&lt;/p&gt;
&lt;div class=&quot;div-red&quot;&gt;
&lt;span class=&quot;alert-header&quot;&gt;Caution:&lt;/span&gt;
&lt;span class=&quot;alert-body&quot;&gt; Not all objects have hash value. If an object does not have a Hash value, &lt;i&gt;TypeError&lt;/i&gt; exception will be raised if we try to access its Hash value.&lt;/span&gt;
&lt;/div&gt;
&lt;p&gt; &lt;strong&gt;&lt;em&gt;Hashability&lt;/em&gt;&lt;/strong&gt; makes an object usable as a &lt;em&gt;dictionary key&lt;/em&gt; and a &lt;em&gt;set member&lt;/em&gt;, because these data structures use the hash value internally.&lt;br&gt;
Most of Python’s immutable built-in objects are hashable.&lt;/p&gt;
&lt;div class=&quot;div-faq&quot;&gt;
&lt;span class=&quot;faq-header&quot;&gt;FAQ&lt;/span&gt;
&lt;span class=&quot;faq-body&quot;&gt;1) Numbers and strings are hashable.&lt;br&gt;2) Tuples and frozenset are hashable if their individual elements are hashable.&lt;br&gt;3) Mutable datatypes like Lists and Dictionaries are unhashable.&lt;/span&gt;
&lt;/div&gt;
&lt;h3 id=&quot;exploring-the-error&quot;&gt;Exploring the error&lt;/h3&gt;
&lt;p&gt;Let us examine some examples where this error might happen &lt;br&gt;
&lt;img src=&quot;https://logiclair.org/?qa=blob&amp;amp;qa_blobid=9955545177823155010&quot; alt=&quot;&quot;&gt;&lt;/p&gt;
&lt;h3 id=&quot;case-1-using-a-mutable-datatype-as-a-dictionary-key-&quot;&gt;Case 1: Using a mutable datatype as a Dictionary Key:&lt;/h3&gt;
&lt;pre&gt;&lt;code&gt;student ={&quot;name&quot;: &quot;abcd&quot;, &quot;age&quot;: 22}
new_dict = {student:&quot;highschool&quot;}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In the first example, we are trying to use &quot;student&quot; which is a &quot;dictionary data type&quot; as a dictionary key for another dictionary &lt;code&gt;new_dict&lt;/code&gt;.&lt;br&gt;
Dictionary itself is a mutable data type : Hence unhashable&lt;br&gt;
Dictionary keys are Hashed internally by Python, for fast accessibility&lt;br&gt;
The code is trying to hash an &lt;strong&gt;unhashable &lt;code&gt;student&lt;/code&gt; dictionary&lt;/strong&gt; so that it can be used as a dictionary key. Hence the error&lt;/p&gt;
&lt;h3 id=&quot;case-2-using-a-mutable-datatype-as-an-element-of-a-set-&quot;&gt;Case 2: Using a mutable datatype as an element of a Set:&lt;/h3&gt;
&lt;pre&gt;&lt;code&gt;set = {&quot;aa&quot;, &quot;bb&quot;, &quot;cc&quot;, &quot;dd&quot;, student}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In the second example, we are trying to use a dictionary data type &lt;code&gt;student&lt;/code&gt; as an element of a Set.&lt;br&gt;
As already mentioned above, Dictionary is a mutable data type: Hence unhashable. Elements of a set are hashed, for fast accessibility&lt;br&gt;
This is the reason we get this error&lt;/p&gt;
&lt;p&gt;Similar errors like: &lt;strong&gt;&lt;em&gt;“TypeError: unhashable type: ‘list’&lt;/em&gt;&lt;/strong&gt; also occur because of the same reason. Lists are mutable, hence unusable as dictionary keys, or set elements&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;my_set = {[1,2]}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&quot;https://logiclair.org/?qa=blob&amp;amp;qa_blobid=10549794984628199301&quot; alt=&quot;&quot;&gt;&lt;/p&gt;
&lt;hr&gt;
&lt;h2 id=&quot;solution&quot;&gt;Solution&lt;/h2&gt;
&lt;p&gt;The solution is to use Immutable datatypes where we require hashability, i.e. when we want them to be used as &lt;strong&gt;elements of sets&lt;/strong&gt; and &lt;strong&gt;keys for dictionary&lt;/strong&gt;. Let us explore some alternate datatypes that can be used when we have a usecase where hashing is required.&lt;/p&gt;
&lt;h3 id=&quot;use-tuple-instead-of-list&quot;&gt;Use Tuple instead of List&lt;/h3&gt;
&lt;p&gt;Suppose we need to use a group of integers [1,2,3] as a dictionary key.&lt;br&gt;
Instead of using  a list [1,2,3] we should convert it into a tuple (1,2,3) . This tuple when used as a dictionary key , would be hashed internally by Python. So, we will not get any error.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;my_list = [1,2,3]
my_dict = {tuple(my_list):1}
my_dict
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&quot;https://logiclair.org/?qa=blob&amp;amp;qa_blobid=14882117849055818606&quot; alt=&quot;&quot;&gt;&lt;/p&gt;
&lt;h3 id=&quot;convert-dictionary-to-tuple&quot;&gt;Convert Dictionary to Tuple&lt;/h3&gt;
&lt;p&gt;Even dictionary can also be converted into tuple and then used as key. Since tuple is hashable, it can definitely be used as dictionary key. Lets consider a simplified example. Here we are converting an unhashable dictionary &lt;code&gt;student&lt;/code&gt; to a hashable tuple &lt;code&gt;student_tuple&lt;/code&gt;. When we use this &lt;code&gt;student_tuple&lt;/code&gt; as a dictionary key python is able to hash it internally and we do not get any error.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;student = {&quot;name&quot;:&quot;abcd&quot;, &quot;age&quot;: 22}
student_tuple = tuple(student.items())
new_dict = {student_tuple:1}
new_dict
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&quot;https://logiclair.org/?qa=blob&amp;amp;qa_blobid=13838243118740672312&quot; alt=&quot;&quot;&gt;&lt;/p&gt;
&lt;p&gt;The items may also be sorted before being converted into a tuple, to ensure consistent hashing as the dictionary elements are inherently unordered collections.&lt;/p&gt;
&lt;p&gt;So, a better method would be:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;student_tuple = tuple(sorted(student.items()))
&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;use-frozenset-instead-of-set&quot;&gt;Use frozenset instead of set&lt;/h3&gt;
&lt;p&gt;Instead of a set we may use a &lt;a rel=&quot;nofollow&quot; href=&quot;https://docs.python.org/3/library/stdtypes.html#frozenset&quot;&gt;frozenset&lt;/a&gt;, when hashing is required. In python, frozenset are the immutable version of sets. So, the frozenset can definitely be hashed. Here we are converting an unhashable set &lt;code&gt;my_immutable_object&lt;/code&gt; to a frozenset which is hashable. Then when we use it as a dictionary key in &lt;code&gt;my_dict&lt;/code&gt; . Python is able to hash it internally. Hence, we would not get any error.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;my_immutable_object = frozenset({1,2,3})
my_dict = {my_immutable_object:1}
my_dict
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&quot;https://logiclair.org/?qa=blob&amp;amp;qa_blobid=4657973929722010427&quot; alt=&quot;&quot;&gt;&lt;/p&gt;
&lt;p&gt;  &lt;/p&gt;&lt;div class=&quot;div-green&quot;&gt;&lt;br&gt;
&lt;span class=&quot;alert-header&quot;&gt;Tips:&lt;/span&gt;&lt;br&gt;
&lt;span class=&quot;alert-body&quot;&gt;1) When using dictionaries, ensure that its keys are immutable datatypes &lt;br&gt; 2) When using sets, ensure that all its elements are immutable datatypes&lt;br&gt;&lt;br&gt;
3) If there is a use case where a mutable datatype like list, set, or dictionary should be used as a dictionary key or set element, first convert it into an immutable datatype like tuple or frozenset&lt;br&gt;
&lt;/span&gt;&lt;br&gt;
&lt;/div&gt; 
&lt;hr&gt;
&lt;h2&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;In a nutshell, the understanding of mutability and hashing will help avoid this type of errors. It is important to choose your datatypes carefully and apply appropriate functions to them, considering factors like mutablity and hashability. Hope this article gives sufficient insight into solving this type of error.&lt;/p&gt;
</description>
<category>Articles</category>
<guid isPermaLink="true">https://coderlegion.com/7686/understanding-and-debugging-typeerror-unhashable-type-dict</guid>
<pubDate>Sun, 23 Nov 2025 23:58:18 +0000</pubDate>
</item>
</channel>
</rss>