My App
Phase 5 — Python-specific Features

List Comprehension

A one-line way to build a list — Python's version of .map() and .filter()

A list comprehension builds a new list from an existing iterable, in a single line.

JavaScript:

const numbers = [1, 2, 3, 4, 5];
const squares = numbers.map((x) => x * x);

Python:

numbers = [1, 2, 3, 4, 5]

squares = [x * x for x in numbers]

Read it as: "x * x, for every x in numbers". The general shape is:

[expression for item in iterable]

Same idea as .map(), just written as one expression instead of a callback.

With a condition

Add if at the end to filter values as you go — combining what .filter().map() would take two steps to do in JS:

const evenSquares = numbers.filter((x) => x % 2 === 0).map((x) => x * x);
even_squares = [x * x for x in numbers if x % 2 == 0]
[expression for item in iterable if condition]

You'll see this shape constantly once you start reading real Python code — it looks unusual at first, but it's just a compact for loop that builds a list.

On this page