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

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:

python
{key_expression: value_expression for item in iterable if condition}
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)
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

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.

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

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

Swipe to start coding

A traveler wants to filter their travel_wishlist to include only destinations in Japan. To achieve this efficiently, you need to extract relevant city-budget pairs into a dictionary.

  • Filter the travel_wishlist to include only destinations where the country is "Japan".
  • Store the resulting dictionary in japan_destinations.

Solution

# Given travel wishlist
travel_wishlist = [['Paris', 'France', 2000],['Tokyo', 'Japan', 3000],['New York', 'USA', 2500],
['Kyoto', 'Japan', 1500],['Rome', 'Italy', 2200],['Sydney', 'Australia', 2800],
['Barcelona', 'Spain', 1900],['London', 'UK', 2600],['Berlin', 'Germany', 2100],
['Dubai', 'UAE', 3500],['Bangkok', 'Thailand', 1800],['Singapore', 'Singapore', 2900],
['Los Angeles', 'USA', 2700],['Cape Town', 'South Africa', 2300],['Venice', 'Italy', 2000],
['Istanbul', 'Turkey', 1750],['Toronto', 'Canada', 2250],['Rio de Janeiro', 'Brazil', 1950],
['Athens', 'Greece', 1850]]

# Filter destinations in Japan using dictionary comprehension
japanese_destinations = {city: budget for city, country, budget in travel_wishlist if country == "Japan"}

# Testing
print('Japanese Destinations:', japanese_destinations)
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 4. Chapter 4
# Given travel wishlist
travel_wishlist = [['Paris', 'France', 2000],['Tokyo', 'Japan', 3000],['New York', 'USA', 2500],
['Kyoto', 'Japan', 1500],['Rome', 'Italy', 2200],['Sydney', 'Australia', 2800],
['Barcelona', 'Spain', 1900],['London', 'UK', 2600],['Berlin', 'Germany', 2100],
['Dubai', 'UAE', 3500],['Bangkok', 'Thailand', 1800],['Singapore', 'Singapore', 2900],
['Los Angeles', 'USA', 2700],['Cape Town', 'South Africa', 2300],['Venice', 'Italy', 2000],
['Istanbul', 'Turkey', 1750],['Toronto', 'Canada', 2250],['Rio de Janeiro', 'Brazil', 1950],
['Athens', 'Greece', 1850]]

# Filter destinations in Japan using dictionary comprehension
japanese_destinations = {}

# Testing
print('Japanese Destinations:', japanese_destinations)

Ask AI

expand
ChatGPT

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

some-alt