{"id":115157,"date":"2020-07-08T06:51:54","date_gmt":"2020-07-08T06:51:54","guid":{"rendered":"https:\/\/blog.finxter.com\/?p=10576"},"modified":"2020-07-08T06:51:54","modified_gmt":"2020-07-08T06:51:54","slug":"how-to-create-a-list-of-dictionaries-in-python","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2020\/07\/08\/how-to-create-a-list-of-dictionaries-in-python\/","title":{"rendered":"How to Create a List of Dictionaries in Python?"},"content":{"rendered":"<p><iframe loading=\"lazy\" height=\"400px\" width=\"100%\" src=\"https:\/\/repl.it\/@finxter\/listofdicts?lite=true\" scrolling=\"no\" frameborder=\"no\" allowtransparency=\"true\" allowfullscreen=\"true\" sandbox=\"allow-forms allow-pointer-lock allow-popups allow-same-origin allow-scripts allow-modals\"><\/iframe> <\/p>\n<p><strong>Problem<\/strong>: Say, you have a <a href=\"https:\/\/blog.finxter.com\/python-dictionary\/\" target=\"_blank\" rel=\"noreferrer noopener\" title=\"Python Dictionary \u2013 The Ultimate Guide\">dictionary <\/a><code>{0: 'Alice', 1: 'Bob'}<\/code> and you want to create a <a href=\"https:\/\/blog.finxter.com\/merge-dictionaries\/\" target=\"_blank\" rel=\"noreferrer noopener\" title=\"Python How to Join a List of Dictionaries into a Single One?\">list of dictionaries<\/a> with <a href=\"https:\/\/blog.finxter.com\/python-list-copy\/\" target=\"_blank\" rel=\"noreferrer noopener\" title=\"Python List copy()\">copies <\/a>of the original dictionary: <code>[{0: 'Alice', 1: 'Bob'}, {0: 'Alice', 1: 'Bob'}, {0: 'Alice', 1: 'Bob'}]<\/code>. <\/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=\"\">d = {0: 'Alice', 1: 'Bob'} dicts = [{**d} for _ in range(3)]\nprint(dicts)\n# [{0: 'Alice', 1: 'Bob'}, {0: 'Alice', 1: 'Bob'}, {0: 'Alice', 1: 'Bob'}]<\/pre>\n<p>You use<a href=\"https:\/\/blog.finxter.com\/list-comprehension\/\" target=\"_blank\" rel=\"noreferrer noopener\" title=\"List Comprehension in Python \u2014 A Helpful Illustrated Guide\"> list comprehension<\/a> with a &#8220;throw-away&#8221; <a href=\"https:\/\/blog.finxter.com\/python-loops\/\" target=\"_blank\" rel=\"noreferrer noopener\" title=\"Python Loops\">loop <\/a>variable underscore <code>_<\/code> to create a list of 3 elements. You can change the value 3 if you need more or fewer elements in your <a href=\"https:\/\/blog.finxter.com\/python-lists\/\" target=\"_blank\" rel=\"noreferrer noopener\" title=\"The Ultimate Guide to Python Lists\">list<\/a>. <\/p>\n<p>The expression <code>{**d}<\/code> unpacks all (key, value) pairs from the original dictionary <code>d<\/code> into a new dictionary. <a href=\"https:\/\/blog.finxter.com\/what-is-asterisk-in-python\/\" target=\"_blank\" rel=\"noreferrer noopener\" title=\"What is the Asterisk \/ Star Operator (*) in Python?\">For more information about the unpacking operator, see this Finxter blog tutorial. <\/a><\/p>\n<p>The resulting list contains copies of the original dictionary. If you change one, the others won&#8217;t see that change:<\/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=\"\">dicts[0][2] = 'Frank'\nprint(dicts)\n# [{0: 'Alice', 1: 'Bob', 2: 'Frank'}, {0: 'Alice', 1: 'Bob'}, {0: 'Alice', 1: 'Bob'}]<\/pre>\n<p>Only the first dictionary in the list contains the new key value pair <code>(2: 'Frank')<\/code> which proves that the dictionaries don&#8217;t point to the same object in memory. This would be the case if you&#8217;d use the following method of <a href=\"https:\/\/blog.finxter.com\/copy-list-of-lists-in-python-shallow-vs-deep\/\" target=\"_blank\" rel=\"noreferrer noopener\" title=\"How to Copy List of Lists in Python (Shallow vs Deep)?\">copying a list<\/a> with a single dictionary:<\/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=\"\">d2 = {0: 'Alice', 1: 'Bob'} dicts2 = [d2] * 3\nprint(dicts2)\n# [{0: 'Alice', 1: 'Bob'}, {0: 'Alice', 1: 'Bob'}, {0: 'Alice', 1: 'Bob'}]<\/pre>\n<p>The method looks right but all three dictionaries are essentially the same:<\/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=\"\">dicts2[0][2] = 'Frank'\nprint(dicts2)\n# [{0: 'Alice', 1: 'Bob', 2: 'Frank'}, {0: 'Alice', 1: 'Bob', 2: 'Frank'}, {0: 'Alice', 1: 'Bob', 2: 'Frank'}]\n<\/pre>\n<p>If you change one, you change all.<\/p>\n<p>You can see this effect yourself in the following memory visualizer tool:<\/p>\n<p> <iframe loading=\"lazy\" src=\"https:\/\/pythontutor.com\/iframe-embed.html#code=d2%20%3D%20%7B0%3A%20'Alice',%201%3A%20'Bob'%7D%0A%0Adicts2%20%3D%20%5Bd2%5D%20*%203%0Aprint%28dicts2%29%0A%0Adicts2%5B0%5D%5B2%5D%20%3D%20'Frank'%0Aprint%28dicts2%29&amp;codeDivHeight=400&amp;codeDivWidth=350&amp;cumulative=false&amp;curInstr=1&amp;heapPrimitives=nevernest&amp;origin=opt-frontend.js&amp;py=3&amp;rawInputLstJSON=%5B%5D&amp;textReferences=false\" width=\"800\" height=\"500\" frameborder=\"0\"> <\/iframe> <\/p>\n<p><em><strong>Exercise<\/strong>: change the method to the correct one so that the change affects only the first dictionary!<\/em><\/p>\n<p><strong>Related articles<\/strong>: <a href=\"https:\/\/blog.finxter.com\/how-to-create-a-python-list\/\" target=\"_blank\" rel=\"noreferrer noopener\" title=\"How to Create a Python List?\">How to create a Python list?<\/a><\/p>\n<h2>Where to Go From Here?<\/h2>\n<p>Enough theory, let\u2019s get some practice!<\/p>\n<p>To become successful in coding, you need to get out there and solve real problems for real people. That\u2019s how you can become a six-figure earner easily. And that\u2019s how you polish the skills you really need in practice. After all, what\u2019s the use of learning theory that nobody ever needs?<\/p>\n<p><strong>Practice projects is how you sharpen your saw in coding!<\/strong><\/p>\n<p>Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?<\/p>\n<p>Then become a Python freelance developer! It\u2019s the best way of approaching the task of improving your Python skills\u2014even if you are a complete beginner.<\/p>\n<p>Join my free webinar <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/webinar-freelancer\/\" target=\"_blank\">\u201cHow to Build Your High-Income Skill Python\u201d<\/a> and watch how I grew my coding business online and how you can, too\u2014from the comfort of your own home.<\/p>\n<p><a href=\"https:\/\/blog.finxter.com\/webinar-freelancer\/\" target=\"_blank\" rel=\"noreferrer noopener\">Join the free webinar now!<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem: Say, you have a dictionary {0: &#8216;Alice&#8217;, 1: &#8216;Bob&#8217;} and you want to create a list of dictionaries with copies of the original dictionary: [{0: &#8216;Alice&#8217;, 1: &#8216;Bob&#8217;}, {0: &#8216;Alice&#8217;, 1: &#8216;Bob&#8217;}, {0: &#8216;Alice&#8217;, 1: &#8216;Bob&#8217;}]. d = {0: &#8216;Alice&#8217;, 1: &#8216;Bob&#8217;} dicts = [{**d} for _ in range(3)] print(dicts) # [{0: &#8216;Alice&#8217;, 1: [&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-115157","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\/115157","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=115157"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/115157\/revisions"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=115157"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=115157"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=115157"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}