Editor settings

General settings

Save settings

Learn to program with Python

Update a dictionary #

Write a function that takes two dictionaries as input and updates the first dictionary with key-value pairs from the second dictionary. If a key already exists in the first dictionary, the value should be updated with the value from the second dictionary. If a key doesn't exist in the first dictionary, it should be added.

Note #

The method dict.update() is not allowed for this challenge.


Tests #

  • update_dictionary({"name": "John", "age": 30}, {"age": 32, "city": "New York"}) should return {"name": "John", "age": 32, "city": "New York"}
  • update_dictionary({"a": 1, "b": 2, "c": 3}, {"b": 4, "d": 5}) should return {"a": 1, "b": 4, "c": 3, "d": 5}
  • update_dictionary({"x": 100, "y": 200}, {}) should return {"x": 100, "y": 200}
  • update_dictionary({}, {"name": "Alice", "age": 25}) should return {"name": "Alice", "age": 25}
  • update_dictionary({"a": 10, "b": 20, "c": 30}, {"b": 25, "c": 35, "d": 40}) should return {"a": 10, "b": 25, "c": 35, "d": 40}
  • Must not use dict.update() method
Output will be displayed here