Itertools in Python 3, By Example – Real Python


Some Interesting Things Include:

  1. iter creates an iterator that spits out one element from an iterable at a time. Effectively, it’s a pointer to the next element to be yielded
  2. Elements created by iterators are tuples
  3. Having multiple iters allows one to return “batches” of items:
import itertools as it
 
def batch(iterable, batch_size):
	  iters = [iter(iterable)] * batch_size
    return zip(*iters)
  1. The above won’t work if len(iterable) is a non-integer multiple of batch_size. To fix that, one can use it.zip_longest:
def batch_proper(iterable, batch_size, fillvalue):
    iters = [iter(iterable)] * batch_size
    return it.zip_longest(*iters, fillvalue)
  1. it.count is kinda like range except it always returns an infinite sequence
  2. Similarly, it.islice does slicing but on potentially infinite iterators
  3. it.accumulate accumulates values in an iterable, kinda like how reduce works.
    • The default func argument for it.accumulate is operator.add
  4. it.tee creates n “entangled” iterators, each with its own FIFO queue.
    • When any of the iterator pulls new values from it.tee, those new values are appended to all the other iterator queues created by it.tee as well
    • Is more memory-inefficient if one iterator keeps pulling data