Python Functions and Comprehensions/Filtered list comprehension

Filtered list comprehension

Section: Comprehensions and generators

Filtered list comprehension

python
python
evens = [n for n in range(20) if n % 2 == 0]

Learn the surrounding workflow

Compare similar commands or jump into common fixes when this command is part of a bigger troubleshooting path.

Related commands

Same sheet · prioritizing Comprehensions and generators
List comprehension
List comprehension
OpenIn sheetpythonsame section
Set comprehension
Set comprehension
OpenIn sheetpythonsame section
Nested comprehension
Nested comprehension
OpenIn sheetpythonsame section
Generator expression
Generator expression
OpenIn sheetpythonsame section
Generator function with yield
Generator function with yield
OpenIn sheetpythonsame section
Define a function
Define a function