Comprehensions
Comprehensions build new containers in memory. They’re a great fit when you want to transform or filter an iterable and you actually need the resulting list, set, or dictionary. In contrast, if you only need to iterate over results once, a generator expression may be a better choice.
For comprehensions, these best practices help you balance concision and readability:
- Use comprehensions for quick data transformations. Prefer list, set, or dictionary comprehensions for straightforward data transformations or filtering, where the logic fits comfortably on one line.
- Keep comprehensions flat. Avoid deeply nested comprehensions or ones with several conditions. If a comprehension spans multiple lines with complicated logic, consider breaking it into a few clear
forloops or helper functions instead. - Pick the right comprehension type. Use list comprehensions when order and duplicates matter, set comprehensions when you want unique items without a strict order, and dictionary comprehensions when you need to build mappings from keys to values.
- Avoid side effects inside comprehensions. Comprehensions are most effective for computing values to build a collection, rather than performing I/O operations or mutating external state. Use regular loops for code with side effects.
- Prefer a
forloop when clarity drops. If your comprehension needs multiple conditions, complex expressions, or nested loops, then switching to an explicit loop often makes the intent easier to follow and debug.
To see some of these ideas in practice, check the following example that builds a list of cubes:
🔴 Avoid this:
>>> cubes = []
>>> for number in range(10):
... cubes.append(number**3)
...
>>> cubes
[0, 1, 8, 27, 64, 125, 216, 343, 512, 729]
This code works. It does a basic data transformation inside a loop. However, when your goal is only to build a new list from an iterable, a comprehension is often clearer and more concise.
✅ Favor this:
>>> cubes = [number**3 for number in range(10)]
>>> cubes
[0, 1, 8, 27, 64, 125, 216, 343, 512, 729]
In this version, you use a comprehension to traverse the range of numbers, compute their cubes, and build a new list using a single expression, which is often a single line of code. The comprehension expression is short, readable, and clearly communicates its intent: create a list of cubes from a range of numbers.
Related Resources
Tutorial
When to Use a List Comprehension in Python
Python list comprehensions help you to create lists while performing sophisticated filtering, mapping, and conditional logic on their members. In this tutorial, you'll learn when to use a list comprehension in Python and how to create them effectively.
For additional information on related topics, take a look at the following resources:
- Python Dictionary Comprehensions: How and When to Use Them (Tutorial)
- Python Set Comprehensions: How and When to Use Them (Tutorial)
- Understanding Python List Comprehensions (Course)
- When to Use a List Comprehension in Python (Quiz)
- Building Dictionary Comprehensions in Python (Course)
- Python Dictionary Comprehensions: How and When to Use Them (Quiz)
- Python Set Comprehensions: How and When to Use Them (Quiz)