Add merge_dictionaries
This commit is contained in:
27
snippets/merge_dictionaries.md
Normal file
27
snippets/merge_dictionaries.md
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
---
|
||||||
|
title: merge_dictionaries
|
||||||
|
tags: dictionary,intermediate
|
||||||
|
---
|
||||||
|
|
||||||
|
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 }
|
||||||
|
```
|
||||||
Reference in New Issue
Block a user