Files
30-seconds-of-code/snippets/merge-dictionaries.md
Angelos Chalaris f6a215e9e3 Kebab file names
2023-04-27 22:00:06 +03:00

32 lines
584 B
Markdown

---
title: Merge dictionaries
tags: dictionary
cover: plant-candle
firstSeen: 2020-04-16T19:28:35+03:00
lastUpdated: 2020-11-02T19:28:27+02:00
---
Merges two or more dictionaries.
- Create a new `dict` and loop over `dicts`, using `dictionary.update()` to add the key-value pairs from each one to the result.
```py
def merge_dictionaries(*dicts):
res = dict()
for d in dicts:
res.update(d)
return res
```
```py
ages_one = {
'Peter': 10,
'Isabel': 11,
}
ages_two = {
'Anna': 9
}
merge_dictionaries(ages_one, ages_two)
# { 'Peter': 10, 'Isabel': 11, 'Anna': 9 }
```