Programming & syntax · reviewed in July 2026
List comprehension
A list comprehension is Python's compact syntax for building a new list by applying an expression to every item of an iterable, with an optional conditional filter. It replaces the pattern of creating an empty list and using a for loop with append to fill it.
numbers = [1, 2, 3, 4, 5] even = [n for n in numbers if n % 2 == 0]
Frequently asked questions
What does the basic syntax look like?
The form [expression for item in iterable] — for example [x * 2 for x in numbers] doubles every number in the numbers list.
Can you add a condition to it?
Yes, by adding an if at the end: [x for x in numbers if x > 0] builds a list containing only the positive numbers.
Are they faster than a plain for loop?
They're usually somewhat faster because Python optimizes them internally, but the main reason to use them is shorter, more readable code, not just speed.