{"id":113882,"date":"2020-06-07T09:37:16","date_gmt":"2020-06-07T09:37:16","guid":{"rendered":"https:\/\/blog.finxter.com\/?p=9316"},"modified":"2020-06-07T09:37:16","modified_gmt":"2020-06-07T09:37:16","slug":"python-how-to-join-a-list-of-dictionaries-into-a-single-one","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2020\/06\/07\/python-how-to-join-a-list-of-dictionaries-into-a-single-one\/","title":{"rendered":"Python How to Join a List of Dictionaries into a Single One?"},"content":{"rendered":"<figure class=\"wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\">\n<div class=\"wp-block-embed__wrapper\">\n<div class=\"ast-oembed-container\"><iframe loading=\"lazy\" title=\"Python: How to Merge Multiple Dictionaries?\" width=\"1400\" height=\"788\" src=\"https:\/\/www.youtube.com\/embed\/bkJycO1hor8?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen><\/iframe><\/div>\n<\/div>\n<\/figure>\n<p><strong>Problem<\/strong>: Say, you&#8217;ve got a list of dictionaries:<\/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=\"\">[{'a': 0}, {'b': 1}, {'c': 2}, {'d': 3}, {'e': 4, 'a': 4}]<\/pre>\n<p><em>Notice how the first and the last dictionaries carry the same key <code>'a'<\/code>. <\/em><\/p>\n<p><strong>How do you merge all those dictionaries into a single dictionary to obtain the following one?<\/strong><\/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=\"\">{'a': 4, 'b': 1, 'c': 2, 'd': 3, 'e': 4}<\/pre>\n<p><em>Notice how the value of the duplicate key <code>'a'<\/code> is the value of the last and not the first dict in the list of dicts.<\/em><\/p>\n<p class=\"has-background has-luminous-vivid-amber-background-color\">To merge multiple dictionaries, the most Pythonic way is to use dictionary comprehension <code>{k:v for x in l for k,v in x.items()}<\/code> to first iterate over all dictionaries in the list <code>l<\/code> and then iterate over all (key, value) pairs in each dictionary.<\/p>\n<p>Let&#8217;s explore all the available options in the remaining article:<\/p>\n<p> <iframe loading=\"lazy\" src=\"https:\/\/trinket.io\/embed\/python\/0e07682a4a\" width=\"100%\" height=\"700\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" allowfullscreen><\/iframe> <\/p>\n<p><em><strong>Exercise<\/strong>: Run the code&#8212;which method generates a different output than all the other methods?<\/em><\/p>\n<h2>Method 1: Dictionary Comprehension With Nested Context<\/h2>\n<p>You can use dictionary comprehension <code>{k:v for x in l for k,v in x.items()}<\/code> to first iterate over all dictionaries in the list <code>l<\/code> and then iterate over all (key, value) pairs in each dictionary. <\/p>\n<ul>\n<li>Create a new dictionary using the <code>{...}<\/code> notation.<\/li>\n<li>Go over all dictionaries in the list of dictionaries <code>l<\/code> by using the outer loop <code>for x in l<\/code>. <\/li>\n<li>Go over all (key, value) pairs in the current dictionary <code>x<\/code> by using the <code>x.items()<\/code> method that returns an iterable of (key, value) pairs.<\/li>\n<li>Fill the new dictionary with (key, value) pairs <code>k:v<\/code> using the general dictionary comprehension syntax <code>{k:v for ...}<\/code>.<\/li>\n<\/ul>\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=\"\">l = [{'a': 0}, {'b': 1}, {'c': 2}, {'d': 3}, {'e': 4, 'a': 4}] d = {k:v for x in l for k,v in x.items()}\nprint(d)\n# {'a': 4, 'b': 1, 'c': 2, 'd': 3, 'e': 4}<\/pre>\n<p>This is the most Pythonic way to merge multiple dictionaries into a single one and it works for an arbitrary number of dictionaries.<\/p>\n<h2>Method 2: Simple Nested Loop<\/h2>\n<p>Use a simple nested loop to add each (key, value) pair separately to a newly created dictionary:<\/p>\n<ul>\n<li>Create a new, empty dictionary.<\/li>\n<li>Go over each dictionary in the list of dictionaries.<\/li>\n<li>Go over each (key, value) pair in the current dictionary.<\/li>\n<li>Add the (key, value) pair to the new dictionary&#8212;possibly overwriting &#8220;older&#8221; keys with the current one.<\/li>\n<\/ul>\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=\"\">l = [{'a': 0}, {'b': 1}, {'c': 2}, {'d': 3}, {'e': 4, 'a': 4}] d = {}\nfor dictionary in l: for k, v in dictionary.items(): d[k] = v print(d)\n{'a': 4, 'b': 1, 'c': 2, 'd': 3, 'e': 4}<\/pre>\n<p>You can visualize the execution flow of this code here:<\/p>\n<p> <iframe loading=\"lazy\" width=\"800\" height=\"500\" frameborder=\"0\" src=\"https:\/\/pythontutor.com\/iframe-embed.html#code=l%20%3D%20%5B%7B'a'%3A%200%7D,%20%7B'b'%3A%201%7D,%20%7B'c'%3A%202%7D,%20%7B'd'%3A%203%7D,%20%7B'e'%3A%204,%20'a'%3A%204%7D%5D%0A%0Ad%20%3D%20%7B%7D%0Afor%20dictionary%20in%20l%3A%0A%20%20%20%20for%20k,%20v%20in%20dictionary.items%28%29%3A%0A%20%20%20%20%20%20%20%20d%5Bk%5D%20%3D%20v%0A%0Aprint%28d%29&#038;codeDivHeight=400&#038;codeDivWidth=350&#038;cumulative=false&#038;curInstr=0&#038;heapPrimitives=nevernest&#038;origin=opt-frontend.js&#038;py=3&#038;rawInputLstJSON=%5B%5D&#038;textReferences=false\"> <\/iframe> <\/p>\n<p>This is the easiest to read for many beginner coders&#8212;but it&#8217;s much less concise and less Pythonic. <\/p>\n<h2>Method 3: Use the update() Method<\/h2>\n<p>The <code>dict.update(x)<\/code> method updates the dictionary on which it is called with a bunch of new (key, value) pairs given in the dictionary argument <code>x<\/code>. The method to merge multiple dictionaries is simple:<\/p>\n<ul>\n<li>Create a new, empty dictionary.<\/li>\n<li>Go over each dictionary in the list of dictionaries.<\/li>\n<li>Update the new dictionary with the values in the current dictionary.<\/li>\n<\/ul>\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=\"\">l = [{'a': 0}, {'b': 1}, {'c': 2}, {'d': 3}, {'e': 4, 'a': 4}] d = {}\nfor dictionary in l: d.update(dictionary) print(d)\n# {'a': 4, 'b': 1, 'c': 2, 'd': 3, 'e': 4}<\/pre>\n<p>This is a very readable and efficient way and it&#8217;s shorter than method 2. <\/p>\n<h2>Method 4: Dictionary Unpacking<\/h2>\n<p>When applied to a dictionary <code>d<\/code>, the double asterisk operator <code>**d<\/code> unpacks all elements in <code>d<\/code> into the outer dictionary. You can only use this &#8220;dictionary unpacking&#8221; method within an environment (in our case a dictionary) that&#8217;s capable of handling the (key, value) pairs. <\/p>\n<p><em>Side note: Sometimes it&#8217;s also used for <a href=\"https:\/\/blog.finxter.com\/daily-python-puzzle-dictionaries-and-unpacking-arguments\/\" target=\"_blank\" rel=\"noreferrer noopener\">keyword arguments<\/a>. <\/em><\/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=\"\">l = [{'a': 0}, {'b': 1}, {'c': 2}, {'d': 3}, {'e': 4, 'a': 4}] d = {**l[0], **l[1], **l[2], **l[3], **l[4]}\nprint(d)\n# {'a': 4, 'b': 1, 'c': 2, 'd': 3, 'e': 4}<\/pre>\n<p>This is a concise, efficient, and Pythonic way to merge multiple dictionaries. However, it&#8217;s not optimal because you must manually type each unpacking operation. If the dictionary has 100 elements, this wouldn&#8217;t be feasible.<\/p>\n<p><em><strong>Note<\/strong>: you cannot use dictionary unpacking in dictionary comprehension to alleviate this problem&#8212;Python will throw a SyntaxError!<\/em><\/p>\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/06\/image.png\" alt=\"\" class=\"wp-image-9324\" srcset=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/06\/image.png 733w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/06\/image-300x128.png 300w\" sizes=\"(max-width: 733px) 100vw, 733px\" \/><\/figure>\n<h2>Method 5: Use ChainMap With Unpacking<\/h2>\n<p>If you&#8217;re not happy with either of those methods, you can also use the <code>ChainMap<\/code> data structure from the <a rel=\"noreferrer noopener\" href=\"https:\/\/docs.python.org\/3\/library\/collections.html#collections.ChainMap\" target=\"_blank\">collections <\/a>library.<\/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=\"\">l = [{'a': 0}, {'b': 1}, {'c': 2}, {'d': 3}, {'e': 4, 'a': 4}] from collections import ChainMap\nd = dict(ChainMap(*l))\nprint(d)\n# {'e': 4, 'a': 0, 'd': 3, 'c': 2, 'b': 1}<\/pre>\n<p>However, this does not exactly meet our specifications: the fifth dictionary in our collection does not overwrite the (key, value) pairs of the first dictionary. Other than that, it&#8217;s a fast way to merge multiple dictionaries and I wanted to include it here for comprehensibility.<\/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&#8217;ve got a list of dictionaries: [{&#8216;a&#8217;: 0}, {&#8216;b&#8217;: 1}, {&#8216;c&#8217;: 2}, {&#8216;d&#8217;: 3}, {&#8216;e&#8217;: 4, &#8216;a&#8217;: 4}] Notice how the first and the last dictionaries carry the same key &#8216;a&#8217;. How do you merge all those dictionaries into a single dictionary to obtain the following one? {&#8216;a&#8217;: 4, &#8216;b&#8217;: 1, &#8216;c&#8217;: 2, [&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-113882","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\/113882","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=113882"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/113882\/revisions"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=113882"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=113882"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=113882"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}