Get Started

Create dictionary

The countries and capitals lists are again available in the script. It's your job to convert this data to a dictionary where the country names are the keys and the capitals are the corresponding values. As a refresher, here is a recipe for creating a dictionary:

my_dict = {
   "key1":"value1",
   "key2":"value2",
}

In this recipe, both the keys and the values are strings. This will also be the case for this exercise.

This is a part of the course

“Intermediate Python”

View Course

Exercise instructions

  • With the strings in countries and capitals, create a dictionary called europe with 4 key:value pairs. Beware of capitalization! Make sure you use lowercase characters everywhere.
  • Print out europe to see if the result is what you expected.

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']

# From string in countries and capitals, create dictionary europe
europe = {'spain':'madrid', ___, ___, ___ }

# Print europe

This exercise is part of the course

Intermediate Python

BeginnerSkill Level
4.6+
772 reviews

Level up your data science skills by creating visualizations using Matplotlib and manipulating DataFrames with pandas.

Learn about the dictionary, an alternative to the Python list, and the pandas DataFrame, the de facto standard to work with tabular data in Python. You will get hands-on practice with creating and manipulating datasets, and you’ll learn how to access the information you need from these data structures.

Exercise 1: Dictionaries, Part 1Exercise 2: Motivation for dictionariesExercise 3: Create dictionary
Exercise 4: Access dictionaryExercise 5: Dictionaries, Part 2Exercise 6: Dictionary Manipulation (1)Exercise 7: Dictionary Manipulation (2)Exercise 8: DictionariceptionExercise 9: Pandas, Part 1Exercise 10: Dictionary to DataFrame (1)Exercise 11: Dictionary to DataFrame (2)Exercise 12: CSV to DataFrame (1)Exercise 13: CSV to DataFrame (2)Exercise 14: Pandas, Part 2Exercise 15: Square Brackets (1)Exercise 16: Square Brackets (2)Exercise 17: loc and iloc (1)Exercise 18: loc and iloc (2)Exercise 19: loc and iloc (3)

What is DataCamp?

Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.

Start Learning for Free