Loop better: a deeper look at iteration in Python

By Trey Hunner

Elevator Pitch

What’s the difference between an iterable, an iterator, and a generator? Do you know how to create infinitely long iterables?

Come to this talk to learn some of Python’s slightly more advanced looping techniques.

Description

What’s the difference between an iterable, an iterator, and a generator? Do you know how to create infinitely long iterables? Come to this talk to learn some of Python’s slightly more advanced looping techniques.

Iterables are a very big deal in Python and they became even more important in Python 3. There’s quite a bit beyond the basics when it comes to loops and looping in Python. Let’s learn some of Python’s slightly more advanced looping techniques!

In this session, we’ll take a step back and learn about how looping actually works in Python. We’ll then learn about a number of Python looping techniques that you’ve probably overlooked.

We’ll learn about the difference between sequences, iterables, and iterators. We’ll also reveal the iterator protocol that powers for loops in Python.

After we learn the basics, we’ll learn some techniques for working with infinite infinite iterables, generators, and generator expressions.

Attendees will walk away from this session with specific actionable recommendations for refactoring their own code as well as a wealth of new topics to look deeper into after the session.

Notes

Outline:

Looping problems (3 minutes)

  • Looping twice
  • Containment
  • Unpacking

Review: loops and iterables (3 minutes)

  • While loops (over a list)
  • For loops
  • Sequences (lists, strings, tuples)
  • Other iterables (sets, dictionaries)

Looping manually (1 minutes)

  • Looping with indexes only works on sequences
  • Converting to a list doesn’t work for infinitely long iterables

Iterators (2 minutes)

  • We can ask any iterable for an iterator
  • Iterators give us the next item or a StopIteration exception
  • Iterators are very single-purpose (like tally counters)

Looping manually: for real (2 minutes)

  • Implementing a for loop using just a while loop
  • Highlighting iter() and next() usage

Even deeper (4 minutes)

  • Iterators are single-use
  • Iterators are also iterables
  • Iterators are their own iterator
  • Iterators allow lazy looping (generating items as you go)
  • Generators are iterators
  • The power of generators and infinite iterables

Iterators are everywhere (4 minutes)

  • for loops, tuple unpacking, star expressions, etc. use it
  • enumerate, zip
  • generators
  • Django’s QuerySet is not an iterator
  • QuerySet.iterator
  • You can make your own iterators

Thinking lazy (3 minutes)

  • generator expression example
  • itertools.islice example
  • generator function
  • naming unnamed things & re-structuring code

Revisiting our looping problems from the beginning (3 minutes)

  • Looping twice
  • Containment
  • Unpacking

Conclusion (1 minute)

  • Iterables give iterators and iterators give each item individually
  • Iterators are the backbone of looping in Python
  • Iterable doesn’t imply indexes. Sequences are iterables but iterables may not be sequences.
  • When someone says “iterable” they mean “something we can get an iterator from”
  • Look into itertools, boltons, and other libraries and embrace iterables

This talk is aimed at developers who are familiar with for loops, lists, and dictionaries. Knowledge of generators, list comprehensions, or generator expressions would be useful, but none of those concepts are essential. Audience members are not expected to know what an iterable, iterator, or sequence are.

I’ve given variations of this talk at PyCon AU 2017 and at PyGotham 2017.