Learning objectives
By the end of this section you should be able to
- Identify the different components of a list comprehension statement.
- Implement filtering using list comprehension.
List comprehensions
A list comprehension is a Python statement to compactly create a new list using a pattern.
The general form of a list comprehension statement is shown below.
list_name = [expression for loop_variable in iterable]
list_name
refers to the name of a new list, which can be anything, and the for
is the for
loop keyword. An expression defines what will become part of the new list. loop_variable
is an iterator, and iterable
is an object that can be iterated, such as a list or string.
Example 9.5
Creating a new list with a list comprehension
A list comprehension shown below in the second code has the same effect as the regular for
loop shown in the first code. The resultant list is [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
in both cases.
Creating a list of squares using a for
loop.
# Create an empty List.
squares_list = []
# Add items to a list, as squares of numbers starting at 0 and ending at 9.
for i in range(10):
squares_list.append(i*i)
Creating a list of squares using the list comprehension.
square_list = [i*i for i in range(10)]
The expression i*i
is applied for each value of the loop_variable
i
.
Example 9.6
A Dr. Seuss poem
A list comprehension can be used to create a list based on another list. In line 6, the for
loop is written on the list poem_lines
.
1 | # Create a list of words |
2 | words_list = ["one", "two", "red", "blue"] |
3 | |
4 | # Use a list comprehension to create a new list called poem_lines |
5 | # Inserting the word "fish" attached to each word in words_list |
6 | poem_lines = [w + " fish" for w in words_list] |
7 | for line in poem_lines: |
8 | print(line) |
The above code's output is:
one fish two fish red fish blue fish
Concepts in Practice
List comprehensions
Filtering using list comprehensions
List comprehensions can be used to filter items from a given list. A condition is added to the list comprehension.
list_name = [expression for loop_variable in container if condition]
In a filter list comprehension, an element is added into list_name
only if the condition is met.
Concepts in Practice
Filtering using list comprehensions
For each code using list comprehension, select the correct resultant list in new_list
.
Try It
Selecting five-letter words
Write a program that creates a list of only five-letter words from the given list and prints the new list.
Try It
Books starting with "A"
Write a program that selects words that begin with an "A" in the given list. Make sure the new list is then sorted in dictionary order. Finally, print the new sorted list.