06-10-2020, 03:34 PM
Python How to Join a List of Dictionaries into a Single One?
<div><figure class="wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio">
<div class="wp-block-embed__wrapper">
<div class="ast-oembed-container"><iframe 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>
</div>
</figure>
<p><strong>Problem</strong>: Say, you’ve got a list of dictionaries:</p>
<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>
<p><em>Notice how the first and the last dictionaries carry the same key <code>'a'</code>. </em></p>
<p><strong>How do you merge all those dictionaries into a single dictionary to obtain the following one?</strong></p>
<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>
<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>
<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>
<p>Let’s explore all the available options in the remaining article:</p>
<p> <iframe src="https://trinket.io/embed/python/0e07682a4a" width="100%" height="700" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe> </p>
<p><em><strong>Exercise</strong>: Run the code—which method generates a different output than all the other methods?</em></p>
<h2>Method 1: Dictionary Comprehension With Nested Context</h2>
<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>
<ul>
<li>Create a new dictionary using the <code>{...}</code> notation.</li>
<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>
<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>
<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>
</ul>
<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()}
print(d)
# {'a': 4, 'b': 1, 'c': 2, 'd': 3, 'e': 4}</pre>
<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>
<h2>Method 2: Simple Nested Loop</h2>
<p>Use a simple nested loop to add each (key, value) pair separately to a newly created dictionary:</p>
<ul>
<li>Create a new, empty dictionary.</li>
<li>Go over each dictionary in the list of dictionaries.</li>
<li>Go over each (key, value) pair in the current dictionary.</li>
<li>Add the (key, value) pair to the new dictionary—possibly overwriting “older” keys with the current one.</li>
</ul>
<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 = {}
for dictionary in l: for k, v in dictionary.items(): d[k] = v print(d)
{'a': 4, 'b': 1, 'c': 2, 'd': 3, 'e': 4}</pre>
<p>You can visualize the execution flow of this code here:</p>
<p> <iframe 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&codeDivHeight=400&codeDivWidth=350&cumulative=false&curInstr=0&heapPrimitives=nevernest&origin=opt-frontend.js&py=3&rawInputLstJSON=%5B%5D&textReferences=false"> </iframe> </p>
<p>This is the easiest to read for many beginner coders—but it’s much less concise and less Pythonic. </p>
<h2>Method 3: Use the update() Method</h2>
<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>
<ul>
<li>Create a new, empty dictionary.</li>
<li>Go over each dictionary in the list of dictionaries.</li>
<li>Update the new dictionary with the values in the current dictionary.</li>
</ul>
<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 = {}
for dictionary in l: d.update(dictionary) print(d)
# {'a': 4, 'b': 1, 'c': 2, 'd': 3, 'e': 4}</pre>
<p>This is a very readable and efficient way and it’s shorter than method 2. </p>
<h2>Method 4: Dictionary Unpacking</h2>
<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 “dictionary unpacking” method within an environment (in our case a dictionary) that’s capable of handling the (key, value) pairs. </p>
<p><em>Side note: Sometimes it’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>
<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]}
print(d)
# {'a': 4, 'b': 1, 'c': 2, 'd': 3, 'e': 4}</pre>
<p>This is a concise, efficient, and Pythonic way to merge multiple dictionaries. However, it’s not optimal because you must manually type each unpacking operation. If the dictionary has 100 elements, this wouldn’t be feasible.</p>
<p><em><strong>Note</strong>: you cannot use dictionary unpacking in dictionary comprehension to alleviate this problem—Python will throw a SyntaxError!</em></p>
<figure class="wp-block-image size-large"><img 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/uplo...00x128.png 300w" sizes="(max-width: 733px) 100vw, 733px" /></figure>
<h2>Method 5: Use ChainMap With Unpacking</h2>
<p>If you’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>
<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
d = dict(ChainMap(*l))
print(d)
# {'e': 4, 'a': 0, 'd': 3, 'c': 2, 'b': 1}</pre>
<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’s a fast way to merge multiple dictionaries and I wanted to include it here for comprehensibility.</p>
<h2>Where to Go From Here?</h2>
<p>Enough theory, let’s get some practice!</p>
<p>To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?</p>
<p><strong>Practice projects is how you sharpen your saw in coding!</strong></p>
<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>
<p>Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.</p>
<p>Join my free webinar <a rel="noreferrer noopener" href="https://blog.finxter.com/webinar-freelancer/" target="_blank">“How to Build Your High-Income Skill Python”</a> and watch how I grew my coding business online and how you can, too—from the comfort of your own home.</p>
<p><a href="https://blog.finxter.com/webinar-freelancer/" target="_blank" rel="noreferrer noopener">Join the free webinar now!</a></p>
</div>
https://www.sickgaming.net/blog/2020/06/...ingle-one/
<div><figure class="wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio">
<div class="wp-block-embed__wrapper">
<div class="ast-oembed-container"><iframe 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>
</div>
</figure>
<p><strong>Problem</strong>: Say, you’ve got a list of dictionaries:</p>
<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>
<p><em>Notice how the first and the last dictionaries carry the same key <code>'a'</code>. </em></p>
<p><strong>How do you merge all those dictionaries into a single dictionary to obtain the following one?</strong></p>
<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>
<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>
<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>
<p>Let’s explore all the available options in the remaining article:</p>
<p> <iframe src="https://trinket.io/embed/python/0e07682a4a" width="100%" height="700" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe> </p>
<p><em><strong>Exercise</strong>: Run the code—which method generates a different output than all the other methods?</em></p>
<h2>Method 1: Dictionary Comprehension With Nested Context</h2>
<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>
<ul>
<li>Create a new dictionary using the <code>{...}</code> notation.</li>
<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>
<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>
<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>
</ul>
<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()}
print(d)
# {'a': 4, 'b': 1, 'c': 2, 'd': 3, 'e': 4}</pre>
<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>
<h2>Method 2: Simple Nested Loop</h2>
<p>Use a simple nested loop to add each (key, value) pair separately to a newly created dictionary:</p>
<ul>
<li>Create a new, empty dictionary.</li>
<li>Go over each dictionary in the list of dictionaries.</li>
<li>Go over each (key, value) pair in the current dictionary.</li>
<li>Add the (key, value) pair to the new dictionary—possibly overwriting “older” keys with the current one.</li>
</ul>
<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 = {}
for dictionary in l: for k, v in dictionary.items(): d[k] = v print(d)
{'a': 4, 'b': 1, 'c': 2, 'd': 3, 'e': 4}</pre>
<p>You can visualize the execution flow of this code here:</p>
<p> <iframe 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&codeDivHeight=400&codeDivWidth=350&cumulative=false&curInstr=0&heapPrimitives=nevernest&origin=opt-frontend.js&py=3&rawInputLstJSON=%5B%5D&textReferences=false"> </iframe> </p>
<p>This is the easiest to read for many beginner coders—but it’s much less concise and less Pythonic. </p>
<h2>Method 3: Use the update() Method</h2>
<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>
<ul>
<li>Create a new, empty dictionary.</li>
<li>Go over each dictionary in the list of dictionaries.</li>
<li>Update the new dictionary with the values in the current dictionary.</li>
</ul>
<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 = {}
for dictionary in l: d.update(dictionary) print(d)
# {'a': 4, 'b': 1, 'c': 2, 'd': 3, 'e': 4}</pre>
<p>This is a very readable and efficient way and it’s shorter than method 2. </p>
<h2>Method 4: Dictionary Unpacking</h2>
<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 “dictionary unpacking” method within an environment (in our case a dictionary) that’s capable of handling the (key, value) pairs. </p>
<p><em>Side note: Sometimes it’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>
<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]}
print(d)
# {'a': 4, 'b': 1, 'c': 2, 'd': 3, 'e': 4}</pre>
<p>This is a concise, efficient, and Pythonic way to merge multiple dictionaries. However, it’s not optimal because you must manually type each unpacking operation. If the dictionary has 100 elements, this wouldn’t be feasible.</p>
<p><em><strong>Note</strong>: you cannot use dictionary unpacking in dictionary comprehension to alleviate this problem—Python will throw a SyntaxError!</em></p>
<figure class="wp-block-image size-large"><img 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/uplo...00x128.png 300w" sizes="(max-width: 733px) 100vw, 733px" /></figure>
<h2>Method 5: Use ChainMap With Unpacking</h2>
<p>If you’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>
<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
d = dict(ChainMap(*l))
print(d)
# {'e': 4, 'a': 0, 'd': 3, 'c': 2, 'b': 1}</pre>
<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’s a fast way to merge multiple dictionaries and I wanted to include it here for comprehensibility.</p>
<h2>Where to Go From Here?</h2>
<p>Enough theory, let’s get some practice!</p>
<p>To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?</p>
<p><strong>Practice projects is how you sharpen your saw in coding!</strong></p>
<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>
<p>Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.</p>
<p>Join my free webinar <a rel="noreferrer noopener" href="https://blog.finxter.com/webinar-freelancer/" target="_blank">“How to Build Your High-Income Skill Python”</a> and watch how I grew my coding business online and how you can, too—from the comfort of your own home.</p>
<p><a href="https://blog.finxter.com/webinar-freelancer/" target="_blank" rel="noreferrer noopener">Join the free webinar now!</a></p>
</div>
https://www.sickgaming.net/blog/2020/06/...ingle-one/