Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
List Comprehansions with Conditions | List
Python Data Structures
course content

Course Content

Python Data Structures

Python Data Structures

1. List
2. Dictionary
3. Tuple
4. Set

bookList 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 1. Chapter 11
toggle bottom row

bookList 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 1. Chapter 11
toggle bottom row

bookList 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!

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
Section 1. Chapter 11
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