Access dictionary
If the keys of a dictionary are chosen wisely, accessing the values in a dictionary is easy and intuitive. For example, to get the capital for France from europe you can use:
europe['france']
Here, 'france' is the key and 'paris' the value is returned.
This exercise is part of the course
Intermediate Python
Exercise instructions
- Check out which keys are in
europeby calling thekeys()method oneurope. Print out the result. - Print out the value that belongs to the key
'norway'.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Definition of dictionary
europe = {'spain':'madrid', 'france':'paris', 'germany':'berlin', 'norway':'oslo' }
# Print out the keys in europe
# Print out value that belongs to key 'norway'