{"id":128893,"date":"2022-10-14T17:51:24","date_gmt":"2022-10-14T17:51:24","guid":{"rendered":"https:\/\/blog.finxter.com\/?p=789184"},"modified":"2022-10-14T17:51:24","modified_gmt":"2022-10-14T17:51:24","slug":"can-a-python-dictionary-have-a-list-as-a-value","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2022\/10\/14\/can-a-python-dictionary-have-a-list-as-a-value\/","title":{"rendered":"Can a Python Dictionary Have a List as a Value?"},"content":{"rendered":"\n<div class=\"kk-star-ratings kksr-auto kksr-align-left kksr-valign-top\" data-payload=\"{&quot;align&quot;:&quot;left&quot;,&quot;id&quot;:&quot;789184&quot;,&quot;slug&quot;:&quot;default&quot;,&quot;valign&quot;:&quot;top&quot;,&quot;ignore&quot;:&quot;&quot;,&quot;reference&quot;:&quot;auto&quot;,&quot;class&quot;:&quot;&quot;,&quot;count&quot;:&quot;1&quot;,&quot;readonly&quot;:&quot;&quot;,&quot;score&quot;:&quot;5&quot;,&quot;best&quot;:&quot;5&quot;,&quot;gap&quot;:&quot;5&quot;,&quot;greet&quot;:&quot;Rate this post&quot;,&quot;legend&quot;:&quot;5\\\/5 - (1 vote)&quot;,&quot;size&quot;:&quot;24&quot;,&quot;width&quot;:&quot;142.5&quot;,&quot;_legend&quot;:&quot;{score}\\\/{best} - ({count} {votes})&quot;,&quot;font_factor&quot;:&quot;1.25&quot;}\">\n<div class=\"kksr-stars\">\n<div class=\"kksr-stars-inactive\">\n<div class=\"kksr-star\" data-star=\"1\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"2\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"3\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"4\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"5\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div class=\"kksr-stars-active\" style=\"width: 142.5px;\">\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/div>\n<div class=\"kksr-legend\" style=\"font-size: 19.2px;\"> 5\/5 &#8211; (1 vote) <\/div>\n<\/div>\n<h2>Question<\/h2>\n<p class=\"has-global-color-8-background-color has-background\"><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f4ac.png\" alt=\"\ud83d\udcac\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/> <strong>Question<\/strong>: Can you use lists <em>as values<\/em> of a dictionary in Python?<\/p>\n<p>This short article will answer your question. So, let&#8217;s get started right away with the answer:<\/p>\n<h2>Answer<\/h2>\n<p class=\"has-global-color-8-background-color has-background\">You can use <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-lists\/\" data-type=\"post\" data-id=\"7332\" target=\"_blank\">Python lists<\/a> as dictionary values. In fact, you can use arbitrary Python objects as <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-dictionary\/\" data-type=\"post\" data-id=\"5232\" target=\"_blank\">dictionary<\/a> values and all <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-__hash__-magic-method\/\" data-type=\"post\" data-id=\"38118\" target=\"_blank\">hashable<\/a> objects as dictionary keys. You can define a list <code>[1, 2]<\/code> as a dict value either with <code>dict[key] = [1, 2]<\/code> or with <code>d = {key: [1, 2]}<\/code>.<\/p>\n<p>Here&#8217;s a concrete example showing how to create a dictionary <code>friends<\/code> where each dictionary value is in fact a list of friends:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"1,2,3\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">friends = {'Alice': ['Bob', 'Carl'], 'Bob': ['Alice'], 'Carl': []} print('Alice friends: ', friends['Alice'])\n# Alice friends: ['Bob', 'Carl'] print('Bob friends: ', friends['Bob'])\n# Bob friends: ['Alice'] print('Carl friends: ', friends['Carl'])\n# Carl friends: []<\/pre>\n<p>Note that you can also assign lists as values of specific keys by using the <a href=\"https:\/\/blog.finxter.com\/python-dictionary-how-to-create-add-replace-retrieve-remove\/\" data-type=\"post\" data-id=\"36698\" target=\"_blank\" rel=\"noreferrer noopener\">dictionary assignment<\/a> operation like so:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"2,3,4\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">friends = dict()\nfriends['Alice'] = ['Bob', 'Carl']\nfriends['Bob'] = ['Alice']\nfriends['Carl'] = [] print('Alice friends: ', friends['Alice'])\n# Alice friends: ['Bob', 'Carl'] print('Bob friends: ', friends['Bob'])\n# Bob friends: ['Alice'] print('Carl friends: ', friends['Carl'])\n# Carl friends: []\n<\/pre>\n<h2>Can I Use Lists as Dict Keys?<\/h2>\n<p class=\"has-global-color-8-background-color has-background\">You cannot use lists as dictionary keys because lists are <a href=\"https:\/\/blog.finxter.com\/mutable-vs-immutable-objects-in-python\/\" data-type=\"post\" data-id=\"204090\" target=\"_blank\" rel=\"noreferrer noopener\">mutable<\/a> and therefore not <a href=\"https:\/\/blog.finxter.com\/python-__hash__-magic-method\/\" data-type=\"post\" data-id=\"38118\" target=\"_blank\" rel=\"noreferrer noopener\">hashable<\/a>. As dictionaries are built on hash tables, all keys must be hashable or Python raises an <a href=\"https:\/\/blog.finxter.com\/how-to-fix-typeerror-unhashable-type-list\/\" data-type=\"post\" data-id=\"465745\" target=\"_blank\" rel=\"noreferrer noopener\">error message<\/a>.<\/p>\n<p>Here&#8217;s an example:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"3\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">d = dict()\nmy_list = [1, 2, 3]\nd[my_list] = 'abc'\n<\/pre>\n<p>This leads to the following error message:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Traceback (most recent call last): File \"C:\\Users\\xcent\\Desktop\\code.py\", line 3, in &lt;module> d[my_list] = 'abc'\nTypeError: unhashable type: 'list'<\/pre>\n<p class=\"has-global-color-8-background-color has-background\">To fix this, <a href=\"https:\/\/blog.finxter.com\/convert-list-to-tuple\/\" data-type=\"post\" data-id=\"7862\" target=\"_blank\" rel=\"noreferrer noopener\">convert the list to a Python tuple<\/a> and use the Python tuple as a dictionary key. Python tuples are immutable and hashable and, therefore, can be used as set elements or dictionary keys.<\/p>\n<p>Here&#8217;s the same example after converting the list to a <a href=\"https:\/\/blog.finxter.com\/the-ultimate-guide-to-python-tuples\/\" data-type=\"post\" data-id=\"12043\" target=\"_blank\" rel=\"noreferrer noopener\">tuple<\/a>&#8212;it works! <img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f389.png\" alt=\"\ud83c\udf89\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"3-4\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">d = dict()\nmy_list = [1, 2, 3]\nmy_tuple = tuple(my_list)\nd[my_tuple] = 'abc'\n<\/pre>\n<p>Before you go, maybe you want to join our free <a href=\"https:\/\/blog.finxter.com\/email-academy\/\" data-type=\"page\" data-id=\"12278\" target=\"_blank\" rel=\"noreferrer noopener\">email academy<\/a> of ambitious learners like you? The goal is to become 1% better every single day (as a coder). We also have cheat sheets! <img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f447.png\" alt=\"\ud83d\udc47\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/> <\/p>\n","protected":false},"excerpt":{"rendered":"<p>5\/5 &#8211; (1 vote) Question Question: Can you use lists as values of a dictionary in Python? This short article will answer your question. So, let&#8217;s get started right away with the answer: Answer You can use Python lists as dictionary values. In fact, you can use arbitrary Python objects as dictionary values and all [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[857],"tags":[73,468,528],"class_list":["post-128893","post","type-post","status-publish","format-standard","hentry","category-python-tut","tag-programming","tag-python","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/128893","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/comments?post=128893"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/128893\/revisions"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=128893"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=128893"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=128893"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}