Created
July 30, 2020 16:20
-
-
Save raeq/4f18177a23d8a4d412cebf9f31a49b09 to your computer and use it in GitHub Desktop.
Merge two dictionaries.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def merge_dictionaries(left: dict, right: dict) -> dict: | |
"""Merge two dictionaries using a shallow copy.""" | |
temp: dict = left.copy() | |
temp.update(right) | |
return temp | |
countries: dict = { | |
"Taiwan": 36193, | |
"Canada": 9984670, | |
"United States": 9525067, | |
"Russia": 17098246, | |
"Argentina": 2780400, | |
"Zambia": 752612, | |
"China": 9596961, | |
} | |
cities: dict = { | |
"Toronto": ["Canada", 6082000], | |
"New York City": ["United States", 18819000], | |
"Moscow": ["Russia", 12410000], | |
"Buenos Aires": ["Argentina", 14967000], | |
"Shanghai": ["China", 25582000], | |
"Lusaka": ["Zambia", 1747152], | |
"Taipei": ["Taiwan", 2646204], | |
} | |
assert merge_dictionaries(countries, cities) == { | |
"Taiwan": 36193, | |
"Canada": 9984670, | |
"United States": 9525067, | |
"Russia": 17098246, | |
"Argentina": 2780400, | |
"Zambia": 752612, | |
"China": 9596961, | |
"Toronto": ["Canada", 6082000], | |
"New York City": ["United States", 18819000], | |
"Moscow": ["Russia", 12410000], | |
"Buenos Aires": ["Argentina", 14967000], | |
"Shanghai": ["China", 25582000], | |
"Lusaka": ["Zambia", 1747152], | |
"Taipei": ["Taiwan", 2646204], | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment