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'))
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')
The get
method replaces __getitem__
(which you normally access via bracket notation). capitals.get(capitals[country], 'not available')
is actually performing two lookups:
capitals[country]
finds the capital, e.g. Canberra.
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
.
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
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'))
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')
The get
method replaces __getitem__
(which you normally access via bracket notation). capitals.get(capitals[country], 'not available')
is actually performing two lookups:
capitals[country]
finds the capital, e.g. Canberra.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
.
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