August 2019
Beginner
482 pages
12h 56m
English
chain allows multiple iterables to be added together. This is especially useful if both iterables are generators, as chain does not execute them. Consider this example. First, we create two iterables—a generator and a string:
from itertools import chaingenerator = range(3)iterable = 'Python'
Now, we chain them together, and iterate over them:
>>> for el in chain(generator, iterable):>>> print(el)012Python
As you can see, the two are merged together seamlessly. chain is a fast operation that can be extremely useful in certain cases.