Python: Dictionary inside get()

Question:

first post!
I am slowly working my way through Automate the Boring Stuff with Python and am just doing a bit of experimentation with the get() method and dictionaries (chapter 5). I have written a small piece of code to tell me the capital of the country that I type in or, if it’s not in my dictionary, that the capital city is “not available”.

However, the response “not available” is appearing even when I input countries that are included in my dictionary. Any insights about what is going on here? I tried Googling about using dictionaries inside the get() method but didn’t find much that explained the issue. Code as follows:

capitals = {'Australia': 'Canberra', 'England': 'London', 'South Africa': 'Pretoria'}
print('Choose country')
country = input()
print('The capital of ' + country + ' is ' 
      + capitals.get(capitals[country], 'not available'))
Asked By: Foz

||

Answers:

Here is the Synax for get() method:

dict.get(key, default = None)

key in your dictionary is country like ‘Australia’.

In your code:

capitals.get(capitals[country], 'not available')

capitals[country] is not the key, you need to use it like this code:

capitals.get(country, 'not available')
Answered By: blanksky

The get method replaces __getitem__ (which you normally access via bracket notation). capitals.get(capitals[country], 'not available') is actually performing two lookups:

  1. capitals[country] finds the capital, e.g. Canberra.
  2. capitals.get(...) then looks up the name of the capital as the country. There are very few cases where this won’t fail.

If you look up a country that does not exist, capitals[country] will just raise a KeyError.

What you probably meant to do was

capitals.get(country, 'not available')

One more consideration you may want to address is that Python dictionaries are case sensitive. You probably want to get Canberra back whether you pass in Australia, australia, or aUstrAlIA. The standard way of doing this is to make the keys of the dictionary all lowercase, and then look up with either the lowercase or case-folded version:

capitals = {'australia': 'Canberra', 'england': 'London', 'south africa': 'Pretoria'}
country = input('Choose country: ')
print('The capital of', country, 'is', capitals.get(country.casefold(), 'not available'))

Notice that I replaced the first print with an argument to input, and removed the + operators in the second print.

Answered By: Mad Physicist

Here is a simple edit on the code above:

capitals = {'Australia': 'Canberra', 'England': 'London', 'South Africa': 'Pretoria'}
country = input('Insert country > ').title()
output = capitals.get(country, 'Not Available in Dict')
print(f'The capital of {country} is {output}')

Here are the edits :

  • Use the .title() method to convert any country name entered into a "title" format so it will match the naming format in the dictionary
  • Create a variable to store the capital name="key value" before printing it out. The variable here is output
Answered By: Netero
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.