chain

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 chain

generator = range(3)
iterable = 'Python'

Now, we chain them together, and iterate over them:

>>> for el in chain(generator, iterable):
>>> print(el)

0
1
2
P
y
t
h
o
n

As you can see, the two are merged together seamlessly. chain is a fast operation that can be extremely useful in certain cases.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset