Python Itertools.chain.from_iterable() Function with Examples

Itertools Module:

Itertools is a Python module that contains a collection of functions for dealing with iterators. They make it very simple to iterate through iterables such as lists and strings.

Itertools.chain.from_iterable() Function:

chain.from_iterable() belongs to the category of terminating iterators. This function takes a single iterable as an argument, and all of the elements of the input iterable must also be iterable, and it returns a flattened iterable containing all of the input iterable’s elements.

Syntax:

chain.from_iterable(iterable)

Parameters

iterable: It could be any iterable like list, string, and so on.

Examples:

Example1:

Input:

Given List = ["good", "morning", "all"]

Output:

The flattened list of the given list:
['g', 'o', 'o', 'd', 'm', 'o', 'r', 'n', 'i', 'n', 'g', 'a', 'l', 'l']

Example2:

Input:

Given List = ["hello", ['a', 'l', 'l']]

Output:

The flattened list of the given list:
['h', 'e', 'l', 'l', 'o', 'a', 'l', 'l']

Itertools.chain.from_iterable() Function with Examples in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Import itertools module using the import keyword.
  • Give the list as static input and store it in a variable.
  • Pass the given list as an argument to the itertools.chain.from_iterable() function that returns a flattened list (list of characters) containing all of the given list elements.
  • Store it in another variable.
  • Convert the above result into a list using the list() function and store it in a variable.
  • Print the flattened list of the given list.
  • The Exit of the Program.

Below is the implementation:

# Import itertools module using the import keyword.
import itertools
# Give the list as static input and store it in a variable.
gvn_lst = ["good", "morning", "all"]
# Pass the given list as an argument to the itertools.chain.from_iterable()
# function that returns a flattened list (list of characters) containing all
# of the given list elements.
# Store it in another variable.
rslt_chain = itertools.chain.from_iterable(gvn_lst)
# Convert the above result into a list using the list() function and store it
# in a variable.
rslt_lst = list(rslt_chain)
# Print the flattened list of the given list.
print("The flattened list of the given list:")
print(rslt_lst)

Output:

The flattened list of the given list:
['g', 'o', 'o', 'd', 'm', 'o', 'r', 'n', 'i', 'n', 'g', 'a', 'l', 'l']
string and List as one input at a time
# Import itertools module using the import keyword.
import itertools
# Give the list as static input and store it in a variable.
gvn_lst = ["hello", ['a', 'l', 'l']]
# Pass the given list as an argument to the itertools.chain.from_iterable()
# function that returns a flattened list (list of characters) containing all
# of the given list elements.
# Store it in another variable.
rslt_chain = itertools.chain.from_iterable(gvn_lst)
# Convert the above result into a list using the list() function and store it
# in a variable.
rslt_lst = list(rslt_chain)
# Print the flattened list of the given list.
print("The flattened list of the given list:")
print(rslt_lst)

Output:

The flattened list of the given list:
['h', 'e', 'l', 'l', 'o', 'a', 'l', 'l']

Method #2: Using Built-in Functions (User Input)

Approach:

  • Import itertools module using the import keyword.
  • Give the list as user input using list(), input(),and split() functions.
  • Store it in a variable.
  • Pass the given list as an argument to the itertools.chain.from_iterable() function that returns a flattened list (list of characters) containing all of the given list elements.
  • Store it in another variable.
  • Convert the above result into a list using the list() function and store it in a variable.
  • Print the flattened list of the given list.
  • The Exit of the Program.

Below is the implementation:

# Import itertools module using the import keyword.
import itertools
# Give the list as user input using list(),input(),and split() functions.
# Store it in a variable.
gvn_lst = list(input(
   'Enter some random List Elements separated by spaces = ').split())

# Pass the given list as an argument to the itertools.chain.from_iterable()
# function that returns a flattened list (list of characters) containing all
# of the given list elements.
# Store it in another variable.
rslt_chain = itertools.chain.from_iterable(gvn_lst)
# Convert the above result into a list using the list() function and store it
# in a variable.
rslt_lst = list(rslt_chain)
# Print the flattened list of the given list.
print("The flattened list of the given list:")
print(rslt_lst)

Output:

Enter some random List Elements separated by spaces = python code
The flattened list of the given list:
['p', 'y', 't', 'h', 'o', 'n', 'c', 'o', 'd', 'e']