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
5. For deleting

bookList Comprehensions

Hi, welcome to the last but certainly not least chapter of the section on list functionality. List comprehensions are one of the favorite Pythonic ways for Python developers to create lists in a single line.

In essence, list comprehensions can be used to generate lists by applying functions to each element in the list.

Here is the general syntax:

For example:

This is roughly equivalent to the for-loop:

12345
squares = [] for x in (0, 1, 2, 3, 4, 5): squares.append(x*x) print(squares)
copy

List Comprehansions with Conditions

You can also use conditions:

For example:

Which is equivalent to:

123456
squares = [] for x in (0, 1, 2, 3, 4, 5): if x % 2 == 0: squares.append(x*x) print(squares)
copy

Let's practice:

Task

Suppose you have a list of temperatures in Fahrenheit and you want to convert them to Celsius.

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 Comprehensions

Hi, welcome to the last but certainly not least chapter of the section on list functionality. List comprehensions are one of the favorite Pythonic ways for Python developers to create lists in a single line.

In essence, list comprehensions can be used to generate lists by applying functions to each element in the list.

Here is the general syntax:

For example:

This is roughly equivalent to the for-loop:

12345
squares = [] for x in (0, 1, 2, 3, 4, 5): squares.append(x*x) print(squares)
copy

List Comprehansions with Conditions

You can also use conditions:

For example:

Which is equivalent to:

123456
squares = [] for x in (0, 1, 2, 3, 4, 5): if x % 2 == 0: squares.append(x*x) print(squares)
copy

Let's practice:

Task

Suppose you have a list of temperatures in Fahrenheit and you want to convert them to Celsius.

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 Comprehensions

Hi, welcome to the last but certainly not least chapter of the section on list functionality. List comprehensions are one of the favorite Pythonic ways for Python developers to create lists in a single line.

In essence, list comprehensions can be used to generate lists by applying functions to each element in the list.

Here is the general syntax:

For example:

This is roughly equivalent to the for-loop:

12345
squares = [] for x in (0, 1, 2, 3, 4, 5): squares.append(x*x) print(squares)
copy

List Comprehansions with Conditions

You can also use conditions:

For example:

Which is equivalent to:

123456
squares = [] for x in (0, 1, 2, 3, 4, 5): if x % 2 == 0: squares.append(x*x) print(squares)
copy

Let's practice:

Task

Suppose you have a list of temperatures in Fahrenheit and you want to convert them to Celsius.

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!

Hi, welcome to the last but certainly not least chapter of the section on list functionality. List comprehensions are one of the favorite Pythonic ways for Python developers to create lists in a single line.

In essence, list comprehensions can be used to generate lists by applying functions to each element in the list.

Here is the general syntax:

For example:

This is roughly equivalent to the for-loop:

12345
squares = [] for x in (0, 1, 2, 3, 4, 5): squares.append(x*x) print(squares)
copy

List Comprehansions with Conditions

You can also use conditions:

For example:

Which is equivalent to:

123456
squares = [] for x in (0, 1, 2, 3, 4, 5): if x % 2 == 0: squares.append(x*x) print(squares)
copy

Let's practice:

Task

Suppose you have a list of temperatures in Fahrenheit and you want to convert them to Celsius.

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
some-alt