Skip to content
This repository was archived by the owner on May 7, 2023. It is now read-only.

Files

Latest commit

 

History

History
31 lines (27 loc) · 564 Bytes

merge-dictionaries.md

File metadata and controls

31 lines (27 loc) · 564 Bytes
title type tags cover dateModified
Merge dictionaries
snippet
dictionary
plant-candle
2020-11-02 19:28:27 +0200

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.
def merge_dictionaries(*dicts):
  res = dict()
  for d in dicts:
    res.update(d)
  return res
ages_one = {
  'Peter': 10,
  'Isabel': 11,
}
ages_two = {
  'Anna': 9
}
merge_dictionaries(ages_one, ages_two)
# { 'Peter': 10, 'Isabel': 11, 'Anna': 9 }