List Comprehension in Python — a practical guide to Python list comprehension with clear examples you can reuse in real projects.
Python Tutorial Series (25/95). Prefer one article? Read the complete Python tutorial.
Short description
List comprehensions replace many short for-loops. You can also add conditions.
Comprehension
squares = [n * n for n in range(1, 6)]
evens = [n for n in range(10) if n % 2 == 0]
print(squares, evens)