- 5.1 Introduction
- 5.2 Lists
- 5.3 Tuples
- 5.4 Unpacking Sequences
- 5.5 Sequence Slicing
- 5.6 del Statement
- 5.7 Passing Lists to Functions
- 5.8 Sorting Lists
- 5.9 Searching Sequences
- 5.10 Other List Methods
- 5.11 Simulating Stacks with Lists
- 5.12 List Comprehensions
- 5.13 Generator Expressions
- 5.14 Filter, Map and Reduce
- 5.15 Other Sequence Processing Functions
- 5.16 Two-Dimensional Lists
- 5.17 Intro to Data Science: Simulation and Static Visualizations
- 5.18 Wrap-Up
5.12 List Comprehensions
Here, we continue discussing functional-style features with list comprehensions—a concise and convenient notation for creating new lists. List comprehensions can replace many for statements that iterate over existing sequences and create new lists, such as:
In [1]: list1 = [] In [2]: for item in range(1, 6): ...: list1.append(item) ...: In [3]: list1 Out[3]: [1, 2, 3, 4, 5]
Using a List Comprehension to Create a List of Integers
We can accomplish the same task in a single line of code with a list comprehension:
In [4]: list2 = [item for item in range(1, 6)] In [5]: list2 Out[5]: [1, 2, 3, 4, 5]
Like snippet [2]’s for statement, the list comprehension’s for clause
for item in range(1, 6)
iterates over the sequence produced by range(1, 6). For each item, the list comprehension evaluates the expression to the left of the for clause and places the expression’s value (in this case, the item itself) in the new list. Snippet [4]’s particular comprehension could have been expressed more concisely using the function list:
list2 = list(range(1, 6))
Mapping: Performing Operations in a List Comprehension’s Expression
A list comprehension’s expression can perform tasks, such as calculations, that map elements to new values (possibly of different types). Mapping is a common functional-style programming operation that produces a result with the same number of elements as the original data being mapped. The following comprehension maps each value to its cube with the expression item ** 3:
In [6]: list3 = [item ** 3 for item in range(1, 6)] In [7]: list3 Out[7]: [1, 8, 27, 64, 125]
Filtering: List Comprehensions with if Clauses
Another common functional-style programming operation is filtering elements to select only those that match a condition. This typically produces a list with fewer elements than the data being filtered. To do this in a list comprehension, use the if clause. The following includes in list4 only the even values produced by the for clause:
In [8]: list4 = [item for item in range(1, 11) if item % 2 == 0] In [9]: list4 Out[9]: [2, 4, 6, 8, 10]
List Comprehension That Processes Another List’s Elements
The for clause can process any iterable. Let’s create a list of lowercase strings and use a list comprehension to create a new list containing their uppercase versions:
In [10]: colors = ['red', 'orange', 'yellow', 'green', 'blue'] In [11]: colors2 = [item.upper() for item in colors] In [12]: colors2 Out[12]: ['RED', 'ORANGE', 'YELLOW', 'GREEN', 'BLUE'] In [13]: colors Out[13]: ['red', 'orange', 'yellow', 'green', 'blue']