Open In App

Python - Itertools.chain.from_iterable()

Last Updated : 12 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report
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
  1. Functions producing Infinite Iterators
  2. Functions producing Iterators terminating on the shortest input sequence
  3. 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: Python3 1==
# Importing chain class from itertools
from itertools import chain


# Single iterable containing iterable
# elements(strings) is passed as input
from_iterable = chain.from_iterable(['geeks',
                                     'for', 
                                     'geeks'])

# printing the flattened iterable
print(list(from_iterable))
Output :
['g', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'g', 'e', 'e', 'k', 's']
Example #2: Python3 1==
# Importing chain class from itertools
from itertools import chain


# Single iterable containing iterable
# elements(strings and list) is passed
# as input
from_iterable = chain.from_iterable(['geeks', 
                                     'for',
                                     'geeks',
                                     ['w', 'i', 'n', 's']])

# printing the flattened iterable
print(list(from_iterable))
Output :
['g', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'g', 'e', 'e', 'k', 's', 'w', 'i', 'n', 's']

Next Article
Article Tags :
Practice Tags :

Similar Reads

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy