<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>Coder Legion - Recent posts tagged mutable</title>
<link>https://coderlegion.com/tag/mutable</link>
<description>Powered by Question2Answer</description>
<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 id=&quot;conclusion&quot;&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>