List comprehension in Python is a concise and readable way to create new lists by applying an expression to each item in an existing iterable (like a list, tuple, or range), optionally filtering items based on a condition.
Basic Syntax
[expression for item in iterable]expression: The operation applied to each item (e.g.,
x ** 2).item: The variable representing each element in the iterable.
iterable: The source of data (e.g.,
range(10),[1, 2, 3]).
With Filtering (if condition)
[expression for item in iterable if condition]Only items satisfying the
conditionare included in the result.
Examples
Square numbers:
squares = [x ** 2 for x in range(5)] # [0, 1, 4, 9, 16]Filter even numbers:
evens = [x for x in range(10) if x % 2 == 0] # [0, 2, 4, 6, 8]Transform strings:
uppercase = [word.upper() for word in ['hello', 'world']] # ['HELLO', 'WORLD']Flatten nested lists:
matrix = [[1, 2], [3, 4]] flattened = [num for row in matrix for num in row] # [1, 2, 3, 4]
Benefits
More readable: Combines looping, filtering, and transformation in one line.
Faster execution: Often faster than equivalent
forloops due to internal optimizations.Cleaner code: Eliminates the need for manual
append()calls and temporary variables.
When to Avoid
Avoid deeply nested or complex logic—readability decreases.
For large datasets, consider generator expressions
( )instead of list comprehensions[]to save memory.
Related Structures
Set comprehension:
{x for x in range(5)}→{0, 1, 2, 3, 4}Dictionary comprehension:
{k: v for k, v in [('a', 1), ('b', 2)]}→{'a': 1, 'b': 2}
List comprehensions are a core Pythonic idiom for clean, efficient list creation.
Can Anyone Quickly Explain the Basics of List Comprehension Please?
Python list function or list comprehension - Stack Overflow
Does anyone else hate list comprehension?
I don't understand list comprehensions using if else statements.
Videos
Even Chat-GPT and my dog can't explain it right. I think I might be unintelligent.
There is a slight bit of difference in their performance as can be seen from the following:
squares1 = [x**2 for x in range(1, 11)]
3.07 µs ± 70 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
squares2 = list(x**2 for x in range(1, 11))
3.65 µs ± 35.6 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
this can primarily be because in case 1 you are iterating and generating values for the list at the same time.
In case 2 you are generating values while iterating and then at the end of it converting the same to a list and this is then stored as a given variable.
I way I see it, first program directly initializes squares1 as a list through list comprehension.
The other one first creates a generator class object and then converts it into a list. I think first approach is more efficient.
There are little differences between lists and generators but as per my knowledge and experience lists do the job faster and generators do it lazily yielding single result for every iteration. For most of your tasks, I'd recommend opting for lists.