Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Dictionary Comprehensions | List and Dictionary Comprehensions
Python Loops Tutorial
course content

Course Content

Python Loops Tutorial

Python Loops Tutorial

1. The for Loop
2. The while Loop
3. Nested Loops
4. List and Dictionary Comprehensions

book
Dictionary Comprehensions

Dictionary comprehensions provide a concise way to create dictionaries in Python, similar to list comprehensions. They enable you to transform data into key-value pairs in a single line of code.

At its heart, a basic dictionary comprehension lets you construct a new dictionary by applying an expression to each key-value pair in an iterable variable. The syntax:

  • key_expression: defines the key for each key-value pair;
  • value_expression: defines the value corresponding to the key;
  • iterable: the source of items to be processed (e.g., a list, range, or another iterable).
12345678910111213141516
travel_wishlist = [ ["Paris", 2000], ["Tokyo", 3000], ["New York", 2500], ["Kyoto", 1500], ["Sydney", 4000] ] # Initialize an empty dictionary travel_budget = {} # Populate the dictionary using a for loop for destination, cost in travel_wishlist: travel_budget[destination] = cost print(travel_budget)
copy

Description:

This code iterates through the travel_wishlist list, where each sublist contains a destination and its budget. The for loop assigns the destination as the key and the budget as the value in the travel_budget dictionary.

Example: Creating a Travel Budget Dictionary Using Dictionary Comprehension

123456789101112
travel_wishlist = [ ["Paris", 2000], ["Tokyo", 3000], ["New York", 2500], ["Kyoto", 1500], ["Sydney", 4000] ] # Create the dictionary using dictionary comprehension travel_budget = {destination: cost for destination, cost in travel_wishlist} print(travel_budget)
copy

Description:

This example uses dictionary comprehension to achieve the same result as the previous example. Each destination becomes a key, and its corresponding cost becomes the value in the travel_budget dictionary, all in a single line.

Task
test

Swipe to begin your solution

A traveler wants to create a dictionary that maps city names to their corresponding countries. Use dictionary comprehension to create a new dictionary called city_to_country from the following travel_wishlist:

Transform the travel_wishlist list into a dictionary where the city is the key and the country is the value.

Solution

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 4. Chapter 3
toggle bottom row

book
Dictionary Comprehensions

Dictionary comprehensions provide a concise way to create dictionaries in Python, similar to list comprehensions. They enable you to transform data into key-value pairs in a single line of code.

At its heart, a basic dictionary comprehension lets you construct a new dictionary by applying an expression to each key-value pair in an iterable variable. The syntax:

  • key_expression: defines the key for each key-value pair;
  • value_expression: defines the value corresponding to the key;
  • iterable: the source of items to be processed (e.g., a list, range, or another iterable).
12345678910111213141516
travel_wishlist = [ ["Paris", 2000], ["Tokyo", 3000], ["New York", 2500], ["Kyoto", 1500], ["Sydney", 4000] ] # Initialize an empty dictionary travel_budget = {} # Populate the dictionary using a for loop for destination, cost in travel_wishlist: travel_budget[destination] = cost print(travel_budget)
copy

Description:

This code iterates through the travel_wishlist list, where each sublist contains a destination and its budget. The for loop assigns the destination as the key and the budget as the value in the travel_budget dictionary.

Example: Creating a Travel Budget Dictionary Using Dictionary Comprehension

123456789101112
travel_wishlist = [ ["Paris", 2000], ["Tokyo", 3000], ["New York", 2500], ["Kyoto", 1500], ["Sydney", 4000] ] # Create the dictionary using dictionary comprehension travel_budget = {destination: cost for destination, cost in travel_wishlist} print(travel_budget)
copy

Description:

This example uses dictionary comprehension to achieve the same result as the previous example. Each destination becomes a key, and its corresponding cost becomes the value in the travel_budget dictionary, all in a single line.

Task
test

Swipe to begin your solution

A traveler wants to create a dictionary that maps city names to their corresponding countries. Use dictionary comprehension to create a new dictionary called city_to_country from the following travel_wishlist:

Transform the travel_wishlist list into a dictionary where the city is the key and the country is the value.

Solution

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 4. Chapter 3
Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
We're sorry to hear that something went wrong. What happened?
some-alt