Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Iterators (Iterables)

You already know that there are all kinds of objects in Python that you can iterate over using the for-in construct. For example, you can iterate over the characters of a string, or the elements of a list, or whatever range() returns. You can also iterate over the lines of a file and you have probably seen the for in construct in other cases as well. The objects that can be iterated over are collectively called iterables. You can do all kind of interesting things on such iterables. We’ll see a few now.

There are several data types that we can iterate over using the for … in … construct. For example

  • strings
  • lists
  • tuples
  • filehandles
  • range