I have the following dictionary and I would like to do a reverse lookup.
countries_dict = {'Andorra': ('Europe', 'Andorra la Vella'),
'Afghanistan': ('Asia', 'Kabul'),
'Antigua and Barbuda': ('North America', "St. John's"),
'Albania': ('Europe', 'Tirana'),
'Armenia': ('Asia', 'Yerevan'),
'Angola': ('Africa', 'Luanda'),
'Argentina': ('South America', 'Buenos Aires'),
'Austria': ('Europe', 'Vienna'),
'Australia': ('Oceania', 'Canberra'),
'Azerbaijan': ('Asia', 'Baku'),
'Barbados': ('North America', 'Bridgetown'),
'Bangladesh': ('Asia', 'Dhaka'),
'Belgium': ('Europe', 'Brussels')}
My code looks like this:
def create_continents_dict(dct = dict) -> dict:
reversed_dictionary = {value[0] : key for (key, value) in dct.items()}
return reversed_dictionary
print(create_continents_dict(countries_dict))
{'Europe': 'Belgium', 'Asia': 'Bangladesh', 'North America': 'Barbados', 'Africa': 'Angola', 'South America': 'Argentina', 'Oceania': 'Australia'}
However I only get the last country it looks up as value. What I want is the value as a list of the countries. With the code above I should expect for every country that has the same continent, it should be in a list right?
{'Europe': ['Andorra', 'Albania', 'Austria', 'Belgium'] 'Asia': ['Afghanistan', 'Armenia', 'Azerbaijan', 'Bangladesh'], 'North America': ['Antigua and Barbuda', 'Barbados'], 'Africa': ['Angola'], 'South America': ['Argentina'], 'Oceania': ['Australia']}