

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Difficulty: 6
click for more info
Not enough gems
Cost: 6 gems
1: Dictionaries
incomplete
2: Duplicate Keys
incomplete
3: Accessing Dictionary Values
incomplete
4: Setting Dictionary Values
incomplete
5: Updating Dictionary Values
incomplete
6: Deleting Dictionary Values
incomplete
7: Counting Practice
incomplete
8: Iterating Over a Dictionary in Python
incomplete
9: Ordered or Unordered?
incomplete
10: Quest Status
incomplete
11: Merge Dictionaries
incomplete
Back
ctrl+,
Next
ctrl+.
This lesson's interactive features are locked, please to keep using them
Guilds can merge in Fantasy Quest. We need to be able to take two player dictionaries (representing guilds) and merge them into a single guild.
Complete the merge function. It accepts two dictionaries as input and returns a new merged dictionary that contains all the keys and values from the input dictionaries.
There are multiple solutions that use built-in Python functions, but we'll use only loops for practice:
If a key exists in both dictionaries, the value from the second dictionary should overwrite the value from the first dictionary. Here's the example usage:
def merge(dict1, dict2):
# implement this
pass
# After merge is implemented:
two_towers = {"Frodo": 56, "Aragorn": 10}
rotk = {"Aragorn": 100, "Gandalf": 809}
merged_dict = merge(two_towers, rotk)
print(merged_dict)
# Output: {'Frodo': 56, 'Aragorn': 100, 'Gandalf': 809}
Notice how the value for the Aragorn key was updated.