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

Swipe to show menu

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.

python
  • 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

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.

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

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

Swipe to start coding

A traveler wants to organize their travel_wishlist by mapping each city name to its corresponding country. To achieve this efficiently, you need to transform the data into a dictionary.

  • Extract city names and their corresponding countries from travel_wishlist.
  • Store the resulting dictionary in city_to_country.

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

Ask AI

expand
ChatGPT

Ask anything or try one of the suggested questions to begin our chat

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.

python
  • 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

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.

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

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

Swipe to start coding

A traveler wants to organize their travel_wishlist by mapping each city name to its corresponding country. To achieve this efficiently, you need to transform the data into a dictionary.

  • Extract city names and their corresponding countries from travel_wishlist.
  • Store the resulting dictionary in city_to_country.

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