← All snippets

List Comprehension One-Liner

Build a new list from an iterable in a single readable line — faster and more Pythonic than a for-loop.

patterns

Code

squares = [x * x for x in range(10) if x % 2 == 0]
# [0, 4, 16, 36, 64]