Skip to lesson
Python for JS Developers

FunctionsLesson 2 of 3

List Comprehensions

3 min

This is the feature that makes Python developers smug. List comprehensions replace map() and filter() with a single, readable expression. Instead of arr.filter(x => x > 5).map(x => x * 2), you write [x * 2 for x in arr if x > 5]. Same result, one clean line.

The syntax reads like English: "give me [expression] FOR each item IN iterable IF condition". Once this pattern locks into your brain, you will reach for it constantly. It works for dictionaries ({k: v for ...}), sets ({x for ...}), and generators ((x for ...)) too.

Generator expressions (with parentheses instead of brackets) are lazy: they compute values one at a time as needed instead of building the entire list in memory. sum(x**2 for x in range(1000000)) processes a million values without storing a million results.

A word of caution: do not make comprehensions too complex. One nested for (like flattening a matrix) plus a simple condition is fine. If you need three for-clauses, several conditions, or you have to read it twice to understand it, use a regular loop. Readability matters more than cleverness. Also avoid side effects inside comprehensions (like calling functions that modify external state). Comprehensions are for building new collections, not for running actions.

Quick reference

JavaScriptPythonNote
arr.map(x => x * 2)[x * 2 for x in arr]List comprehension
arr.filter(x => x > 5)[x for x in arr if x > 5]Add if clause
arr.filter().map()[expr for x in arr if cond]One expression
arr.flat()[x for row in arr for x in row]Nested for, outer loop first
Object.fromEntries(arr.map(...)){k: v for k, v in items}Dict comprehension
Iterator helpers (lazy)(x for x in arr)Generator expression

Side by side

JavaScript
// JavaScript
const nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

const doubled = nums.map(x => x * 2);
const evens = nums.filter(x => x % 2 === 0);
const doubledEvens = nums
  .filter(x => x % 2 === 0)
  .map(x => x * 2);

const matrix = [[1,2],[3,4],[5,6]];
const flat = matrix.flat();

const lengths = Object.fromEntries(
  ["hello", "world"].map(w => [w, w.length])
);
Python
# Python List Comprehensions
nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

doubled = [x * 2 for x in nums]
evens = [x for x in nums if x % 2 == 0]
doubled_evens = [x * 2 for x in nums if x % 2 == 0]

matrix = [[1,2],[3,4],[5,6]]
flat = [x for row in matrix for x in row]

# Dict + Set comprehension
lengths = {w: len(w) for w in ["hello", "world"]}
uniq_len = {len(w) for w in ["hi", "hey", "yo"]}

# Generator (lazy, memory-efficient)
total = sum(x**2 for x in range(1000000))

Exercise

Real Python, running in your browser

Write these using comprehensions (one line each):

1. squares: list of squares of 1 to 20 2. odds: list of only odd numbers from 1 to 50 3. char_map: dict mapping each char in "python" to its uppercase 4. flat: flatten [[1,2,3], [4,5,6], [7,8,9]]

Ctrl + Enter runCtrl + Shift + Enter checkTab indentEsc then Tab to leave the editorCtrl + / comment