Nest all content into snippets

This commit is contained in:
Angelos Chalaris
2023-05-07 16:07:29 +03:00
parent 2ecadbada9
commit 6a45d2ec07
1240 changed files with 0 additions and 0 deletions

View File

@ -0,0 +1,32 @@
---
title: Merge dictionaries
type: snippet
language: python
tags: [dictionary]
cover: plant-candle
dateModified: 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 }
```