Skip to content
dreamcode
dreamcode
Back to map
List comprehensions · Lesson 22 of 44
+15 XP on finish
PYTHON INTERMEDIATE

List comprehensions

A list comprehension is a concise way to create lists. It replaces standard loops with a single line: `[expression for item in iterable if condition]`.

How it reads
[x * 2 for x in nums] loops through nums and returns doubled values
if x % 2 == 0 filters the input list keeping only evens
Cloud tip: Comprehensions are faster and more readable, but do not make them too complex or nested.
main.py
PYTHON
real Python, runs in your browser
CONSOLE
- run your code to see output -