Course Content
Python Loops Tutorial
Python Loops Tutorial
List Comprehansions with Conditions
List comprehensions allow you to filter and process elements efficiently. The syntax:
This syntax helps you create a new list by including only elements that meet a specified condition.
Example: Filtering Cities by Country Using a Traditional for Loop
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']
Description:
This code uses a for
loop and an if
condition to iterate through travel_wishlist
and check if the country is "Japan"
. If the condition is true, the city name is added to japanese_cities
.
Example: Filtering Cities by Country Using List Comprehension
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']
Description:
This example achieves the same result as the previous one but in a more concise way. The list comprehension extracts city names where the country is "Japan"
in a single line of code.
Swipe to begin your solution
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.
Solution
Thanks for your feedback!
List Comprehansions with Conditions
List comprehensions allow you to filter and process elements efficiently. The syntax:
This syntax helps you create a new list by including only elements that meet a specified condition.
Example: Filtering Cities by Country Using a Traditional for Loop
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']
Description:
This code uses a for
loop and an if
condition to iterate through travel_wishlist
and check if the country is "Japan"
. If the condition is true, the city name is added to japanese_cities
.
Example: Filtering Cities by Country Using List Comprehension
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']
Description:
This example achieves the same result as the previous one but in a more concise way. The list comprehension extracts city names where the country is "Japan"
in a single line of code.
Swipe to begin your solution
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.
Solution
Thanks for your feedback!