Get AI summaries of any video or article — Sign up free
Python Tutorial: Comprehensions - How they work and why you should be using them thumbnail

Python Tutorial: Comprehensions - How they work and why you should be using them

Corey Schafer·
5 min read

Based on Corey Schafer's video on YouTube. If you like this content, support the original creators by watching, liking and subscribing to their content.

TL;DR

List comprehensions replace common for-loop patterns with a single expression that reads like “produce this for each item in that.”

Briefing

List comprehensions in Python let programmers build lists, dictionaries, and sets in a compact, readable way—often replacing longer, nested for-loops with a single expression that still mirrors the intent of the code. The core payoff is practical: the syntax stays close to the “for each item in X, produce Y” logic, making common transformations easier to write and easier to scan.

The tutorial starts with the simplest case: copying values from one list to another. A traditional for-loop creates an empty list, appends each number from nums, and prints the result. The list comprehension version does the same work in one line—using brackets and the pattern “expression for item in iterable.” The output matches exactly, but the comprehension is shorter and reads like a direct description of what’s being produced.

Next comes a transformation example: squaring every number in a list. The for-loop appends n * n for each n in nums. The comprehension keeps the same structure—“n * n for n in nums”—so the code communicates both the operation (square) and the iteration source (nums) without the scaffolding of an explicit append loop.

The tutorial also contrasts comprehensions with map and lambda. While map(lambda n: n*n, nums) can produce the same squares, it tends to be harder to read for newcomers because it hides the transformation inside an anonymous function. The message is not that map and lambda are wrong, but that comprehensions usually provide a clearer, more maintainable alternative when the transformation is straightforward.

Filtering is introduced with an even-numbers example. A for-loop checks n % 2 == 0 and appends only those values. The list comprehension adds the condition at the end of the expression: “n for n in nums if n % 2 == 0,” producing the same filtered list in a single line. A similar idea is shown with filter and lambda, but again the comprehension is presented as the more readable option.

The tutorial then demonstrates nested comprehensions. It builds letter-number pairs by iterating letters in ABCD and numbers in range(4). The nested for-loop version uses two loops and appends tuples; the comprehension version nests the “for” clauses inside one expression, yielding the same set of pairs.

From there, comprehensions expand beyond lists. A dictionary comprehension uses braces and a key:value structure, pairing names with superhero names by iterating over zip(names, heroes). A conditional restriction can be appended directly—such as excluding entries where the name equals Peter—without adding extra loop logic.

Finally, set comprehensions use braces without a colon to collect unique values from an input list. The tutorial closes with generator expressions, which look similar to list comprehensions but use parentheses. Generator expressions produce values lazily, and the example shows how they can replace a more verbose generator function plus iteration loop with a single, maintainable line while producing the same sequence of squares.

Cornell Notes

List comprehensions provide a compact, readable way to generate sequences in Python by expressing “what to produce” and “what to iterate over” in one line. The tutorial shows list comprehensions for copying values, transforming values (squaring), and filtering (keeping only even numbers) with conditions appended to the comprehension. It then generalizes the pattern to nested loops for letter-number pairs, and extends it to dictionary comprehensions (using braces with key:value) and set comprehensions (braces with unique values). Generator expressions use parentheses instead of brackets/braces, producing the same results as a generator function but with simpler, more maintainable syntax. Comprehensions also often replace map/lambda and filter/lambda when readability matters.

How does a list comprehension replace a basic for-loop that copies values from one list to another?

A for-loop typically creates an empty list, loops over each item in nums, appends it, and then prints the result. The list comprehension version uses brackets and the pattern: [item for item in iterable]. In the tutorial’s example, the comprehension mirrors the comment-like intent: it returns each n for n in nums, producing a list that matches the original values.

What changes when the goal is to transform each element (like squaring numbers) instead of copying them?

The transformation moves into the “expression” part of the comprehension. A for-loop appends n * n for each n in nums. The list comprehension becomes [n * n for n in nums], so the code simultaneously states the operation (square) and the iteration source (nums). The output matches the for-loop exactly.

How does filtering work in a list comprehension, and how is it different from map/lambda?

Filtering adds an if condition at the end of the comprehension: [expression for item in iterable if condition]. For even numbers, the tutorial uses n % 2 == 0, producing [n for n in nums if n % 2 == 0]. Compared with map/lambda, which applies a function to every element, filtering selectively includes only elements that satisfy the condition—while staying more readable than embedding logic inside lambda.

How can nested loops be expressed inside a single comprehension?

Nested loops appear as multiple “for” clauses inside the comprehension. For letter-number pairs, the tutorial iterates letters in ABCD and numbers in range(4). The comprehension structure is: [(letter, num) for letter in ABCD for num in range(4)]. This replaces two explicit loops that build tuples and append them to a list.

What’s the syntax difference between list, dictionary, and set comprehensions?

List comprehensions use brackets: [expression for ...]. Dictionary comprehensions use braces with key:value: {key_expression: value_expression for ...}. Set comprehensions use braces without a colon: {expression for ...}. The tutorial also shows dictionary creation by iterating over zip(names, heroes), mapping each name to its corresponding hero.

When should a generator expression be used instead of a list comprehension?

Generator expressions use parentheses and produce values lazily, which can be more memory-friendly than building a full list. The tutorial notes that generator expressions are similar in syntax to list comprehensions but require iteration to produce results. The example shows squares generated with (n * n for n in nums), iterated in a for-loop to print each value—matching the behavior of a more verbose generator function.

Review Questions

  1. Write a list comprehension that returns only the odd numbers from a list nums.
  2. Convert a nested for-loop that builds (row, col) pairs into a single comprehension using two for-clauses.
  3. What is the difference in syntax between a dictionary comprehension and a set comprehension?

Key Points

  1. 1

    List comprehensions replace common for-loop patterns with a single expression that reads like “produce this for each item in that.”

  2. 2

    Use the expression portion to transform elements (e.g., [n * n for n in nums] for squares).

  3. 3

    Add an if clause at the end of a list comprehension to filter elements (e.g., if n % 2 == 0).

  4. 4

    Nested comprehensions can replace nested loops by stacking multiple for-clauses inside one comprehension.

  5. 5

    Dictionary comprehensions use braces with key:value pairs, and zip can align two lists element-by-element.

  6. 6

    Set comprehensions use braces without a colon to collect unique values.

  7. 7

    Generator expressions use parentheses and yield values lazily, often simplifying generator-function code.

Highlights

A list comprehension can copy a list in one line while producing identical output to a for-loop that appends each element.
Filtering inside a comprehension uses an appended condition like if n % 2 == 0, avoiding extra loop scaffolding.
Nested comprehensions can generate letter-number tuples by stacking for-clauses in a single expression.
Dictionary comprehensions pair items from two lists using zip(names, heroes) and can exclude entries with an inline if condition.
Generator expressions use parentheses and can replace a generator function plus iteration with a single, readable line.

Topics

  • List Comprehensions
  • Filtering
  • Nested Comprehensions
  • Dictionary Comprehensions
  • Generator Expressions