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

Course Content

Python Data Structures

Python Data Structures

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

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

Creating a New List: Traditional for Loop vs. List Comprehension

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:

12345678910111213
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']
copy

Here, the list comprehension does the same job in a single line, making it concise and readable.

1234567891011
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']
copy

The list comprehension combines the loop and the condition into a single line, making the code easier to understand at a glance.

Task
test

Swipe to show code editor

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.

Note

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

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

Creating a New List: Traditional for Loop vs. List Comprehension

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:

12345678910111213
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']
copy

Here, the list comprehension does the same job in a single line, making it concise and readable.

1234567891011
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']
copy

The list comprehension combines the loop and the condition into a single line, making the code easier to understand at a glance.

Task
test

Swipe to show code editor

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.

Note

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

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

Creating a New List: Traditional for Loop vs. List Comprehension

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:

12345678910111213
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']
copy

Here, the list comprehension does the same job in a single line, making it concise and readable.

1234567891011
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']
copy

The list comprehension combines the loop and the condition into a single line, making the code easier to understand at a glance.

Task
test

Swipe to show code editor

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.

Note

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!

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.

Creating a New List: Traditional for Loop vs. List Comprehension

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:

12345678910111213
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']
copy

Here, the list comprehension does the same job in a single line, making it concise and readable.

1234567891011
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']
copy

The list comprehension combines the loop and the condition into a single line, making the code easier to understand at a glance.

Task
test

Swipe to show code editor

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.

Note

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Section 1. Chapter 10
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