Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
List Comprehansions with Conditions | 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
List Comprehansions with Conditions

You can also use conditions:

For example, suppose you want to create a list of cities located in Japan.

1234567891011121314
travel_wishlist = [ ["Paris", "France", 2000], ["Tokyo", "Japan", 3000], ["New York", "USA", 2500], ["Kyoto", "Japan", 1500], ["Sydney", "Australia", 4000] ] japanese_cities = [] for city in travel_wishlist: if city[1] == "Japan": japanese_cities.append(city[0]) print(japanese_cities) # Output: ['Tokyo', 'Kyoto']
copy

Which is equivalent to:

12345678910
travel_wishlist = [ ["Paris", "France", 2000], ["Tokyo", "Japan", 3000], ["New York", "USA", 2500], ["Kyoto", "Japan", 1500], ["Sydney", "Australia", 4000] ] japanese_cities = [city[0] for city in travel_wishlist if city[1] == "Japan"] print(japanese_cities) # Output: ['Tokyo', 'Kyoto']
copy
Task
test

Swipe to show code editor

Create a new list that contains the names of the cities from your travel_wishlist, but only if the estimated cost is less than $2500. Use a list comprehension.

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

book
List Comprehansions with Conditions

You can also use conditions:

For example, suppose you want to create a list of cities located in Japan.

1234567891011121314
travel_wishlist = [ ["Paris", "France", 2000], ["Tokyo", "Japan", 3000], ["New York", "USA", 2500], ["Kyoto", "Japan", 1500], ["Sydney", "Australia", 4000] ] japanese_cities = [] for city in travel_wishlist: if city[1] == "Japan": japanese_cities.append(city[0]) print(japanese_cities) # Output: ['Tokyo', 'Kyoto']
copy

Which is equivalent to:

12345678910
travel_wishlist = [ ["Paris", "France", 2000], ["Tokyo", "Japan", 3000], ["New York", "USA", 2500], ["Kyoto", "Japan", 1500], ["Sydney", "Australia", 4000] ] japanese_cities = [city[0] for city in travel_wishlist if city[1] == "Japan"] print(japanese_cities) # Output: ['Tokyo', 'Kyoto']
copy
Task
test

Swipe to show code editor

Create a new list that contains the names of the cities from your travel_wishlist, but only if the estimated cost is less than $2500. Use a list comprehension.

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