Python's Itertool is a module that provides various functions that work on iterators to produce complex iterators. This module works as a fast, memory-efficient tool that is used either by themselves or in combination to form iterator algebra.
Note: For more information, refer to
Python Itertools
The functions under itertools can be classified into 3 categories
- Functions producing Infinite Iterators
- Functions producing Iterators terminating on the shortest input sequence
- Functions producing Combinatoric generators
Chain.from_iterable() method
The function
chain.from_iterable() comes under the category of terminating iterators. This function takes a single iterable as an argument and all the elements of the input iterable should also be iterable and it returns a flattened iterable containing all the elements of the input iterable.
Syntax :
chain.from_iterable(iterable)
Example #1:
Output :
['g', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'g', 'e', 'e', 'k', 's']
Example #2: