Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Dictionary Comprehension with Condition | 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 Comprehension with Condition

You can add a condition in a dictionary comprehension to filter items dynamically while creating the dictionary. The syntax for a dictionary comprehension with a condition is:

Example: Filtering Destinations Within a Budget Using a for Loop

12345678910111213141516
travel_wishlist = [ ["Paris", "France", 2000], ["Tokyo", "Japan", 3000], ["New York", "USA", 2500], ["Kyoto", "Japan", 1500], ["Sydney", "Australia", 4000] ] # Filter destinations within a $2500 budget using a for loop affordable_destinations = {} for city, country, budget in travel_wishlist: if budget <= 2500: # Check if the budget is within the limit affordable_destinations[city] = budget print(affordable_destinations)
copy

Description:

This code iterates through the travel_wishlist list, checking if each destination’s budget is less than or equal to $2500. If the condition is met, the city and its budget are added to the affordable_destinations dictionary.

Example: Filtering Destinations Within a Budget Using Dictionary Comprehension

123456789101112
travel_wishlist = [ ["Paris", "France", 2000], ["Tokyo", "Japan", 3000], ["New York", "USA", 2500], ["Kyoto", "Japan", 1500], ["Sydney", "Australia", 4000] ] # Use dictionary comprehension to filter destinations affordable_destinations = {city: budget for city, country, budget in travel_wishlist if budget <= 2500} print(affordable_destinations) # Output: {'Paris': 2000, 'New York': 2500, 'Kyoto': 1500}
copy

Description:

This example achieves the same result as the previous one but in a more concise and elegant way. The condition if budget <= 2500 filters the destinations based on the budget, and the resulting dictionary includes only the affordable destinations.

Task
test

Swipe to begin your solution

A traveler wants to create a dictionary of destinations located in Japan. Use dictionary comprehension to filter the travel_wishlist and include only cities where the country is "Japan". The city should be the dictionary key, and the budget should be the value.

Expected Output:

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 4
toggle bottom row

book
Dictionary Comprehension with Condition

You can add a condition in a dictionary comprehension to filter items dynamically while creating the dictionary. The syntax for a dictionary comprehension with a condition is:

Example: Filtering Destinations Within a Budget Using a for Loop

12345678910111213141516
travel_wishlist = [ ["Paris", "France", 2000], ["Tokyo", "Japan", 3000], ["New York", "USA", 2500], ["Kyoto", "Japan", 1500], ["Sydney", "Australia", 4000] ] # Filter destinations within a $2500 budget using a for loop affordable_destinations = {} for city, country, budget in travel_wishlist: if budget <= 2500: # Check if the budget is within the limit affordable_destinations[city] = budget print(affordable_destinations)
copy

Description:

This code iterates through the travel_wishlist list, checking if each destination’s budget is less than or equal to $2500. If the condition is met, the city and its budget are added to the affordable_destinations dictionary.

Example: Filtering Destinations Within a Budget Using Dictionary Comprehension

123456789101112
travel_wishlist = [ ["Paris", "France", 2000], ["Tokyo", "Japan", 3000], ["New York", "USA", 2500], ["Kyoto", "Japan", 1500], ["Sydney", "Australia", 4000] ] # Use dictionary comprehension to filter destinations affordable_destinations = {city: budget for city, country, budget in travel_wishlist if budget <= 2500} print(affordable_destinations) # Output: {'Paris': 2000, 'New York': 2500, 'Kyoto': 1500}
copy

Description:

This example achieves the same result as the previous one but in a more concise and elegant way. The condition if budget <= 2500 filters the destinations based on the budget, and the resulting dictionary includes only the affordable destinations.

Task
test

Swipe to begin your solution

A traveler wants to create a dictionary of destinations located in Japan. Use dictionary comprehension to filter the travel_wishlist and include only cities where the country is "Japan". The city should be the dictionary key, and the budget should be the value.

Expected Output:

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 4
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