Course Content
Python Data Structures
Python Data Structures
List 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:
squares = [] for x in (0, 1, 2, 3, 4, 5): squares.append(x*x) print(squares)
List Comprehansions with Conditions
You can also use conditions:
For example:
Which is equivalent to:
squares = [] for x in (0, 1, 2, 3, 4, 5): if x % 2 == 0: squares.append(x*x) print(squares)
Let's practice:
Task
Suppose you have a list of temperatures in Fahrenheit and you want to convert them to Celsius.
Thanks for your feedback!
List 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:
squares = [] for x in (0, 1, 2, 3, 4, 5): squares.append(x*x) print(squares)
List Comprehansions with Conditions
You can also use conditions:
For example:
Which is equivalent to:
squares = [] for x in (0, 1, 2, 3, 4, 5): if x % 2 == 0: squares.append(x*x) print(squares)
Let's practice:
Task
Suppose you have a list of temperatures in Fahrenheit and you want to convert them to Celsius.
Thanks for your feedback!
List 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:
squares = [] for x in (0, 1, 2, 3, 4, 5): squares.append(x*x) print(squares)
List Comprehansions with Conditions
You can also use conditions:
For example:
Which is equivalent to:
squares = [] for x in (0, 1, 2, 3, 4, 5): if x % 2 == 0: squares.append(x*x) print(squares)
Let's practice:
Task
Suppose you have a list of temperatures in Fahrenheit and you want to convert them to Celsius.
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:
squares = [] for x in (0, 1, 2, 3, 4, 5): squares.append(x*x) print(squares)
List Comprehansions with Conditions
You can also use conditions:
For example:
Which is equivalent to:
squares = [] for x in (0, 1, 2, 3, 4, 5): if x % 2 == 0: squares.append(x*x) print(squares)
Let's practice:
Task
Suppose you have a list of temperatures in Fahrenheit and you want to convert them to Celsius.