Course Content
Python Loops Tutorial
Python Loops Tutorial
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).
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)
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
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)
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.
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
Thanks for your feedback!
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).
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)
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
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)
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.
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
Thanks for your feedback!