My Docs
BlogGithubLinkedin
Snippets
Snippets
  • Snippets
  • Introduction
  • Python
    • Python
      • take
      • symmetric_difference_by
      • sum_of_powers
      • union
      • transpose
      • unfold
      • tail
      • values_only
      • to_binary
      • take_right
      • find_index
      • months_diff
      • celsius_to_fahrenheit
      • bifurcate
      • days_ago
      • chunk_into_n
      • bifurcate_by
      • add_days
      • intersection_by
      • spread
      • head
      • sum_by
      • initialize_2d_list
      • map_dictionary
      • digitize
      • merge_dictionaries
      • includes_any
  • JavaScript
    • Javascript
      • addEventListenerAll
      • coalesceFactory
      • arrayToHTMLList
      • arithmeticProgression
      • castArray
      • both
      • arrayToCSV
      • atob
      • combine
      • capitalize
      • addMultipleListeners
      • any
      • bifurcate
      • coalesceFactory
Powered by GitBook
On this page

Was this helpful?

  1. Python
  2. Python

merge_dictionaries

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 }
PreviousdigitizeNextincludes_any

Last updated 3 years ago

Was this helpful?