Skip to content

Chapter 3 of 8

Iteration, Generators, and Comprehensions

Python distinguishes iterables — objects that can return an iterator via `__iter__` — from iterators, which implement both `__iter__` and `__next__`. When an iterator is exhausted, it raises `StopIteration`. Comprehensions provide concise syntax for building collections: list comprehensions such as `[x*2 for x in range(10)]` produce lists, while dict and set comprehensions use braces. Generator expressions like `(x*2 for x in range(10))` look similar but evaluate lazily, producing one value at a time without materializing the entire sequence.

Generator functions extend laziness to ordinary functions, using `yield` to return a value while suspending execution state. The `yield from` syntax delegates iteration to another iterable or generator. Built-in iteration tools complete the picture: `range` produces sequences of integers, `enumerate` yields `(index, value)` pairs, and `zip` combines multiple iterables element-wise — stopping at the shortest, unlike `itertools.zip_longest`, which pads with a fill value. `map` applies a function to each item, `filter` retains items where a function returns truthy, and `any` and `all` reduce an iterable to a boolean. `sum`, `min`, and `max` provide aggregate reductions. A list comprehension is generally more readable than the equivalent `list(map(...))` call.

For ordering, `sorted()` returns a new sorted list, while `list.sort()` sorts in place. Both accept a `key` function that extracts the comparison value from each element, and a `reverse=True` flag for descending order. When modifying a list, `append` adds a single item, while `extend` concatenates an iterable. Choosing between eager comprehensions and lazy generators is especially valuable when working with large or potentially infinite sequences.

All chapters
  1. 1Python Foundations and Principles
  2. 2Core Data Types and Type Concepts
  3. 3Iteration, Generators, and Comprehensions
  4. 4Functions, Decorators, and Modules
  5. 5Object-Oriented Programming and Dunder Methods
  6. 6Type Hinting and Modern Language Features
  7. 7Concurrency: GIL, Threads, Processes, and Async
  8. 8Standard Library, Tooling, and Ecosystem

Drill it

Reading is not remembering. These come from the Python Deep Dive deck:

Q

What is Python?

A high-level, interpreted, dynamically typed programming language created by Guido van Rossum in 1991.

Q

What is PEP 8?

Python's official style guide.

Q

What is PEP 20?

The Zen of Python — guiding principles by Tim Peters.

Q

What is the difference between Python 2 and Python 3?

Python 3 is the actively maintained version; Python 2 reached end-of-life in 2020.