liveBook · Manning

Iterable

As explained in Practices of the Python Pro

An iterable is an object or value that provides or supports a protocol for producing its elements sequentially.

An iterable is an object whose contents can be visited sequentially, such as with a Python for loop. Lists, sets, and dictionaries are examples. section 4.2.3

Iterating through n elements takes O(n) time. Collections usually store all their contents in memory, while generators produce one value at a time and avoid storing every result. section 4.2.3

A generator as a memory-friendly iterable

A generator can yield each square only when requested: def squares(items): for item in items: yield item ** 2. This keeps produced values from accumulating in memory. section 4.2.3

Built-in functions also accept iterables. For example, max can inspect a list, set, or dictionary, and its key function can specify what should be compared. section 4.3.2

How Practices of the Python Pro covers it

How deep it goes
Defines iteration, compares collection storage with generators, and explains yield behavior without developing protocol internals.
Examples
Uses for loops, range-like generators, squared-number generators, and maximum-frequency lookup over dictionary data.
What it assumes
Assumes familiarity with Python built-in data structures and for loops while introducing generators and yield.
What you can do after
Apply it via Python loops, generators, and iterable-aware functions while reducing unnecessary memory use.
How it connects
Connects iterables with generators, lists, sets, dictionaries, range, max, key functions, and lambdas.

Also explained in

Compare how 7 books explain iterable →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Practices of the Python Pro. The section links point to the parts of the book it draws on.