First page Back Continue Last page Overview Graphics

Chains

chain(*iterables) --> chain object

Return a chain object which returns elements from the first iterable until it is exhausted, then elements from the next iterable, until all of the iterables are exhausted.

>>> names1 = ["Pete", "Tom"]

>>> names2 = ["Tom", "Oscar"]

>>> names = names1 + names2

names is created by copying the lists names1 and names2. Using chain of itertools is more efficient:

>>> from itertools import chain

>>> names = chain(names1, names2)

>>> for name in names:

... print(name)

...

Pete

Tom

Tom

Oscar