Course Content
Python Loops Tutorial
Python Loops Tutorial
List Comprehensions
List comprehensions are a powerful way to create new lists by combining loops and optional conditions into a single, concise statement. They provide a more Pythonic way to perform operations on lists, making your code cleaner and easier to read.
Example: Creating a New List Using a Traditional for Loop
Let's start with a simple example. You have a travel_wishlist
containing cities you want to visit, each represented as a nested list with its name, country, and trip cost.
So, you need a list with city names only, without countries and trip costs.
For that task you can use for
loop:
travel_wishlist = [ ["Paris", "France", 2000], ["Tokyo", "Japan", 3000], ["New York", "USA", 2500], ["Kyoto", "Japan", 1500], ["Sydney", "Australia", 4000] ] city_names = [] # a new empty list for city in travel_wishlist: city_names.append(city[0]) print(city_names) # Output: ['Paris', 'Tokyo', 'New York', 'Kyoto', 'Sydney']
Example: Creating a New List Using List Comprehension
Here, the list comprehension does the same job in a single line, making it concise and readable.
travel_wishlist = [ ["Paris", "France", 2000], ["Tokyo", "Japan", 3000], ["New York", "USA", 2500], ["Kyoto", "Japan", 1500], ["Sydney", "Australia", 4000] ] city_names = [city[0] for city in travel_wishlist] print(city_names) # Output: ['Paris', 'Tokyo', 'New York', 'Kyoto', 'Sydney']
Explanation:
- The
travel_wishlist
is a list of lists, where each inner list contains the city name, country, and estimated budget for a trip; - The code
[city[0] for city in travel_wishlist]
creates a new list by extracting the first element (city[0]
, the city name) from each inner list intravel_wishlist
.
Swipe to begin your solution
You are managing a travel wishlist, and you need to create a new list that contains only the trip costs (the third element) from each destination in the wishlist.
Solution
Thanks for your feedback!
List Comprehensions
List comprehensions are a powerful way to create new lists by combining loops and optional conditions into a single, concise statement. They provide a more Pythonic way to perform operations on lists, making your code cleaner and easier to read.
Example: Creating a New List Using a Traditional for Loop
Let's start with a simple example. You have a travel_wishlist
containing cities you want to visit, each represented as a nested list with its name, country, and trip cost.
So, you need a list with city names only, without countries and trip costs.
For that task you can use for
loop:
travel_wishlist = [ ["Paris", "France", 2000], ["Tokyo", "Japan", 3000], ["New York", "USA", 2500], ["Kyoto", "Japan", 1500], ["Sydney", "Australia", 4000] ] city_names = [] # a new empty list for city in travel_wishlist: city_names.append(city[0]) print(city_names) # Output: ['Paris', 'Tokyo', 'New York', 'Kyoto', 'Sydney']
Example: Creating a New List Using List Comprehension
Here, the list comprehension does the same job in a single line, making it concise and readable.
travel_wishlist = [ ["Paris", "France", 2000], ["Tokyo", "Japan", 3000], ["New York", "USA", 2500], ["Kyoto", "Japan", 1500], ["Sydney", "Australia", 4000] ] city_names = [city[0] for city in travel_wishlist] print(city_names) # Output: ['Paris', 'Tokyo', 'New York', 'Kyoto', 'Sydney']
Explanation:
- The
travel_wishlist
is a list of lists, where each inner list contains the city name, country, and estimated budget for a trip; - The code
[city[0] for city in travel_wishlist]
creates a new list by extracting the first element (city[0]
, the city name) from each inner list intravel_wishlist
.
Swipe to begin your solution
You are managing a travel wishlist, and you need to create a new list that contains only the trip costs (the third element) from each destination in the wishlist.
Solution
Thanks for your feedback!