SyntaxStudy
Sign Up
Python Intermediate 3 min read

itertools.chain

itertools.chain

itertools.chain() links multiple iterables end-to-end without creating a new list.

Example
from itertools import chain

a = [1, 2, 3]
b = [4, 5, 6]
c = [7, 8, 9]

for val in chain(a, b, c):
    print(val, end=" ")
# 1 2 3 4 5 6 7 8 9
Pro Tip

chain.from_iterable() is useful when you have a list of lists to flatten.