![]() |
|
[Tut] Python How to Join a List of Dictionaries into a Single One? - Printable Version +- Sick Gaming (https://sickgaming.net) +-- Forum: Programming (https://sickgaming.net/forum-76.html) +--- Forum: Python (https://sickgaming.net/forum-83.html) +--- Thread: [Tut] Python How to Join a List of Dictionaries into a Single One? (/thread-95610.html) |
[Tut] Python How to Join a List of Dictionaries into a Single One? - xSicKxBot - 06-10-2020 Python How to Join a List of Dictionaries into a Single One? Problem: Say, you’ve got a list of dictionaries: Code: [{'a': 0}, {'b': 1}, {'c': 2}, {'d': 3}, {'e': 4, 'a': 4}]Notice how the first and the last dictionaries carry the same key Code: 'a'How do you merge all those dictionaries into a single dictionary to obtain the following one? Code: {'a': 4, 'b': 1, 'c': 2, 'd': 3, 'e': 4}Notice how the value of the duplicate key Code: 'a'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: lLet’s explore all the available options in the remaining article: https://trinket.io/embed/python/0e07682a4a Exercise: Run the code—which method generates a different output than all the other methods? Method 1: Dictionary Comprehension With Nested Context You can use dictionary comprehension Code: {k:v for x in l for k,v in x.items()}Code: l
Code: 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}This is the most Pythonic way to merge multiple dictionaries into a single one and it works for an arbitrary number of dictionaries. Method 2: Simple Nested Loop Use a simple nested loop to add each (key, value) pair separately to a newly created dictionary:
Code: 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}You can visualize the execution flow of this code here: 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 This is the easiest to read for many beginner coders—but it’s much less concise and less Pythonic. Method 3: Use the update() Method The Code: dict.update(x)Code: x
Code: 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}This is a very readable and efficient way and it’s shorter than method 2. Method 4: Dictionary Unpacking When applied to a dictionary Code: dCode: **dCode: dSide note: Sometimes it’s also used for keyword arguments. Code: 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}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. Note: you cannot use dictionary unpacking in dictionary comprehension to alleviate this problem—Python will throw a SyntaxError! ![]() Method 5: Use ChainMap With Unpacking If you’re not happy with either of those methods, you can also use the Code: ChainMapCode: 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}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. Where to Go From Here? Enough theory, let’s get some practice! 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? Practice projects is how you sharpen your saw in coding! Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people? 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. Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home. Join the free webinar now! |