List comprehensions

9.5 List comprehensions

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

1.
The component of a list comprehension defining an element of the new list is the _____.
  1. expression
  2. loop_variable
  3. container
2.
What would be the contents of b_list after executing the code below?
a_list = [1, 2, 3, 4, 5]
b_list = [i+2 for i in a_list]
  1. [1, 2, 3, 4, 5]
  2. [0, 1, 2, 3, 4]
  3. [3, 4, 5, 6, 7]
3.
What does new_list contain after executing the statement below?
new_list = [i//3 for i in range(1, 15, 3)]
  1. [0.3333333333333333, 1.3333333333333333, 2.3333333333333335, 3.3333333333333335, 4.333333333333333]
  2. [0, 1, 2, 3, 4]
  3. [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]

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.

Checkpoint

Filtering a list

Concepts in Practice

Filtering using list comprehensions

For each code using list comprehension, select the correct resultant list in new_list.

4.
my_list = [21, -1, 50, -9, 300, -50, 2]

new_list = [m for m in my_list if m < 0]
  1. [21, 50, 300, 2]
  2. [21, -1, 50, -9, 300, -50, 2]
  3. [-1, -9, -50]
5.
my_string = "This is a home."

new_list = [i for i in my_string if i in 'aeiou']
  1. [i, i, a, o, e]
  2. ['i', 'i'', 'a', 'o', 'e']
  3. Error
6.
new_list = [r for r in range (0, 21, 2) if r%2 != 0]
  1. []
  2. [21]
  3. [1, 3, 5, 7, 9, 11, 13, 15, 17, 19]

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.