{"id":2406,"date":"2025-10-03T11:09:59","date_gmt":"2025-10-03T11:09:59","guid":{"rendered":"https:\/\/www.cmarix.com\/qanda\/?p=2406"},"modified":"2026-02-05T11:58:59","modified_gmt":"2026-02-05T11:58:59","slug":"typeerror-unhashable-type-list-python","status":"publish","type":"post","link":"https:\/\/www.cmarix.com\/qanda\/typeerror-unhashable-type-list-python\/","title":{"rendered":"Why do I get a TypeError: Unhashable type: &#8216;list&#8217; When using a list as a Dictionary key or set element?"},"content":{"rendered":"\n<p>Chances are you have run through this error when you tried using a list as a dictionary key or added it to a set, in Python. It looks confusing at first, but it isn&#8217;t as complicated as it might appear. Today we will understand why this error occurs, and how to fix it with ease.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Why This Error Happens<\/h2>\n\n\n\n<p>In Python, dictionary keys and set elements must be hashable. That means:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>They have a fixed hash value.<\/li>\n\n\n\n<li>They can be compared to other objects.<\/li>\n\n\n\n<li>They are not supposed to change (they\u2019re immutable).<\/li>\n<\/ul>\n\n\n\n<p>A list can be changed at any time, which means its value isn\u2019t stable. Because of that, Python doesn\u2019t allow lists to be used as dictionary keys or set elements. It needs something that stays the same, like a tuple.<\/p>\n\n\n\n<p><strong>Here\u2019s what causes the error:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>my_dict = {&#91;1, 2, 3]: \"numbers\"}<\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>TypeError: unhashable type: 'list'<\/code><\/pre>\n\n\n\n<p><strong>Or maybe:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>my_set = set()\nmy_set.add(&#91;1, 2, 3])<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Steps to Resolve the Issue<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Step 1: Use a Tuple Instead of a List<\/h3>\n\n\n\n<p>If your list is just a collection of values and doesn\u2019t need to change, convert it to a tuple:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>my_dict = {(1, 2, 3): \"numbers\"}  # \u2705 No error\n\nmy_set = set()\nmy_set.add((1, 2, 3))  # \u2705 Works<\/code><\/pre>\n\n\n\n<p>Tuples are immutable and hashable if all their elements are hashable.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 2: Avoid Mutable Keys<\/h3>\n\n\n\n<p>Never use mutable types (like lists or dicts) as keys in dictionaries or elements in sets.<\/p>\n\n\n\n<p><strong>Bad:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>my_dict = {&#91;1, 2]: \"value\"}<\/code><\/pre>\n\n\n\n<p><strong>Good:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>my_dict = {tuple(&#91;1, 2]): \"value\"}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Step 3: If You Need Mutability, Use frozenset or Custom Classes<\/h3>\n\n\n\n<p>If your data is inherently set-like but needs to be hashable, use frozenset:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>my_set = set()\nmy_set.add(frozenset(&#91;1, 2, 3]))  # \u2705 Works<\/code><\/pre>\n\n\n\n<p>You can also define your own class with custom __hash__ and __eq__ if advanced control is needed.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 4: Revisit Your Data Structure Design<\/h3>\n\n\n\n<p><strong>Ask yourself: do I really need to use a list here?<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>If yes, maybe switch to another structure (e.g., list of dicts, nested dicts)<\/li>\n\n\n\n<li>If not, replace with a tuple or string depending on the use case<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">To Conclude<\/h2>\n\n\n\n<p>To put it simply, you get this error because lists can change, and Python only allows unchangeable (hashable) types as dictionary keys or set elements. If you need to use list-like data in those places, try using a tuple or frozenset instead. And if you\u2019re unsure about the best structure for your data, it might help to <a href=\"https:\/\/www.cmarix.com\/hire-python-developers.html\">hire Python developers<\/a> who can guide you in building a cleaner, more reliable solution.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Chances are you have run through this error when you tried using a list as a dictionary key or added it to a set, in Python. It looks confusing at first, but it isn&#8217;t as complicated as it might appear. Today we will understand why this error occurs, and how to fix it with ease. [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":2407,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[163,3],"tags":[],"class_list":["post-2406","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","category-web"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/posts\/2406","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/comments?post=2406"}],"version-history":[{"count":2,"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/posts\/2406\/revisions"}],"predecessor-version":[{"id":2410,"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/posts\/2406\/revisions\/2410"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/media\/2407"}],"wp:attachment":[{"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/media?parent=2406"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/categories?post=2406"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/tags?post=2406"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}