Iteration to infinity

Contents
  • count
    • The start parameter
    • The step parameter
  • cycle
  • repeat
    • Infinite repetition
    • The times parameter

This lesson is about functions that produce simple never-ending iterators. The ability to represent concepts like counting upward from one and repeating an item indefinitely is a big part of what distinguishes iterators from lists in Python. In Haskell we describe things like Python iterators as “lazy” because elements are not evaluated until they are needed. Python lists, in contrast, we describe as “strict”.

count

The count functionitertools.count produces a sequence of numbers incrementing by some fixed interval. This iterator is always infinite, so we will use islice to take portions of it when we give examples.

Sign up for access to the full page, plus the complete archive and all the latest content.