Skip to content
dreamcode
dreamcode
Map
List comprehensions
Lesson 31 of 77
+15 XP on finish
PYTHON INTERMEDIATEChapter 6 · Comprehensions and data tools

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].

Worked example

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 its output here.
YOUR TURN

Build squares with a comprehension that holds the square of every number in nums, then print it: [25, 144, 64, 441].

Press Run to check your work.