liveBook · Manning

Iterable

As explained in Python How-To

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

Need to turn task IDs and titles into a dictionary? Both lists are iterables: they supply items one by one. Python can use those items in a loop or pass them to a constructor, often without an indexing loop. section 5.1 section 5.3

An iterable can be converted to an iterator with iter(). Calling next() on that iterator retrieves successive items; after the last item, it raises StopIteration. A for loop creates and advances the iterator for you, then exits when it is exhausted. section 5.1.1

next(tasks_iterator)
# output: 'task0'
 
next(tasks_iterator)
# output: 'task1'
 
next(tasks_iterator)
# output: 'task2'
 
next(tasks_iterator)
# ERROR: StopIteration

Successive next() calls make item-by-item retrieval—and the final StopIteration—visible. section 5.1.1

Strings yield characters. Lists, tuples, dictionaries, sets, and ranges are also iterable. To check an unfamiliar object, try iter(obj): it succeeds for a list but raises TypeError for an integer. A dictionary yields its keys by default; use its values or items when those are what you need. section 2.2.3 section 5.1.2 section 5.5

Build a dictionary from two iterables

Given id_numbers = [101, 102, 103] and titles = ["Laundry", "Homework", "Soccer"], use dict(zip(id_numbers, titles)). The result is {101: "Laundry", 102: "Homework", 103: "Soccer"}. zip pairs corresponding items; dict consumes those pairs. This avoids a loop that indexes both lists. section 5.1 section 5.1.3

Choose the consumer that matches the result you want:

  • Use list(), tuple(), or set() to create a container from an iterable; use dict() when its iterable supplies key–value pairs. For example, list(range(10)) creates a list of numbers, while set((-2, 4, 0, 2, 4, 2)) removes duplicates. section 5.1.3
  • Use list(map(float, numbers_str)) to turn an iterable of numeric strings into a list of floats. If you need to change items while building a container, consider a comprehension; otherwise, try the constructor first. section 5.1.3 section 5.5
  • Use ", ".join(strings) to combine an iterable of strings. The separator owns join(), so the input need not be a list. section 2.3.2

Do not assume that similar-looking objects are interchangeable: range(1, 10, 2) is iterable, but slice(1, 10, 2) is not. A range can feed list() or a for loop; a slice selects part of a sequence. section 4.1.2

How Python How-To covers it

How deep it goes
Explains iteration through iter(), next(), and StopIteration, then tests iterability and works through constructor-based uses.
Examples
Shows task ID–title pairs built with dict(zip()), numeric strings converted with map(), and lists created from ranges.
What it assumes
Assumes familiarity with basic Python containers and for loops while teaching the iterable–iterator distinction explicitly.
What you can do after
Apply it via Python built-ins to test iterability and create or process containers without unnecessary indexing loops.
How it connects
Connects iterables to iterators, for loops, constructors, comprehensions, map, zip, and str.join.

Also explained in

Compare how 7 books explain iterable →

Related concepts

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