Learn Python by Building Data Science Applications
上QQ阅读APP看书,第一时间看更新

itertools

There is a built-in library for working with iterations—itertools. This library provides a large set of sophisticated iterable constructors in three main categories:

  • Infinite iterators: Iterators that allow us to loop an arbitrary number of times through the same collection until certain criteria are met.
  • Terminating on the shortest input sequence: Functions that somehow group iterators, merging or filtering their elements.
  • Combinatoric iterators: These combine multiple iterators, producing a combination of them.

There are a lot of functions, so be sure to check the documentation (https://docs.python.org/3/library/itertools.html). Here, we will discuss just a handful of them.