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.
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)
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.
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)
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 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
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.
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)
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.
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)
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 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
Thanks for your feedback!