Motivation for dictionaries
To see why dictionaries are useful, have a look at the two lists defined in the script. countries
contains the names of some European countries. capitals
lists the corresponding names of their capital.
This exercise is part of the course
Intermediate Python
Exercise instructions
- Use the
index()
method oncountries
to find the index of'germany'
. Store this index asind_ger
. - Use
ind_ger
to access the capital of Germany from thecapitals
list. Print it out.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Definition of countries and capital
countries = ['spain', 'france', 'germany', 'norway']
capitals = ['madrid', 'paris', 'berlin', 'oslo']
# Get index of 'germany': ind_ger
# Use ind_ger to print out capital of Germany