Course Content
Python Data Structures
Python Data Structures
List Comprehansions with Conditions
You can also use conditions:
For example, suppose you want to create a list of cities located in Japan
.
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']
Which is equivalent to:
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']
Thanks for your feedback!
List Comprehansions with Conditions
You can also use conditions:
For example, suppose you want to create a list of cities located in Japan
.
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']
Which is equivalent to:
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']
Thanks for your feedback!
List Comprehansions with Conditions
You can also use conditions:
For example, suppose you want to create a list of cities located in Japan
.
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']
Which is equivalent to:
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']
Thanks for your feedback!
You can also use conditions:
For example, suppose you want to create a list of cities located in Japan
.
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']
Which is equivalent to:
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']