🎁 New User? Get 20% off your first purchase with code NEWUSER20 Register Now →
Menu

Categories

Python Beginner

What is List Comprehension?

A concise syntax for creating new lists by applying an expression to each item in an existing iterable.

List comprehensions provide a readable, Pythonic way to create lists. The syntax is [expression for item in iterable if condition]. For example, [x**2 for x in range(10) if x % 2 == 0] creates a list of squared even numbers.

Similar syntax exists for dictionaries ({k: v for ...}), sets ({x for ...}), and generators ((x for ...)).