SyntaxStudy
Sign Up
Python List Comprehension Basics
Python Beginner 3 min read

List Comprehension Basics

List Comprehension Basics

List comprehensions provide a concise way to create lists by iterating and optionally filtering in a single expression.

Example
squares = [x**2 for x in range(10)]
print(squares)
# [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
Pro Tip

List comprehensions are faster than equivalent for-loops for building lists.