Python Itertools.chain() 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. Chain is one such itertools function ().

Itertools.chain() Function:

It’s a function that takes a list of iterables and returns a single iterable. It combines all of the iterables and returns a single iterable as output. Its output cannot be directly used and must thus be explicitly converted into iterables. This function belongs to the iterators terminating iterators category.

Syntax:

chain (*iterables)

Examples:

Example1:

Input:

Given first List = [9, 8, 7, 6]
Given second List = [40, 30, 20, 10]

Output:

The result after chaining the given two lists:
[9, 8, 7, 6, 40, 30, 20, 10]

Explanation:

Here the chain() function combines the given first and second list and
returns a new list[9, 8, 7, 6, 40, 30, 20, 10]

Example2:

Input:

Given first string = "hello"
Given second string = "ALL"

Output:

The result after chaining the given two strings:
['h', 'e', 'l', 'l', 'o', 'A', 'L', 'L']

Itertools.chain() Function with Examples in Python

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

Approach:

  • Import itertools module using the import keyword.
  • Give the first list as static input and store it in a variable.
  • Give the second list as static input and store it in another variable.
  • Pass the given first and second lists as the arguments to the itertools.chain() method that combines(chains) the given two lists.
  • Store it in another variable.
  • Convert the above result into a list using the list() function and store it in a variable.
  • Print the result after chaining the given two lists.
  • The Exit of the Program.

Below is the implementation:

# Import itertools module using the import keyword.
import itertools
# Give the first list as static input and store it in a variable.
fst_lst = [9, 8, 7, 6]
# Give the second list as static input and store it in another variable.
scnd_lst = [40, 30, 20, 10]
# Pass the given first and second lists as the arguments to the itertools.chain()
# method that combines(chains) the given two lists.
# Store it in another variable.
rslt_chain = itertools.chain(fst_lst, scnd_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 result after chaining the given two lists.
print("The result after chaining the given two lists:")
print(rslt_lst)

Output:

The result after chaining the given two lists:
[9, 8, 7, 6, 40, 30, 20, 10]

For Strings

Approach:

  • Import itertools module using the import keyword.
  • Give the first string as static input and store it in a variable.
  • Give the second string as static input and store it in another variable.
  • Pass the given first and second strings as the arguments to the itertools.chain() method that combines(chains) the given two strings.
  • Store it in another variable.
  • Convert the above result into a list using the list() function and store it in a variable.
  • Print the result after chaining the given two strings.
  • The Exit of the Program.

Below is the implementation:

# Import itertools module using the import keyword.
import itertools
# Give the first string as static input and store it in a variable.
fst_str = "hello"
# Give the second string as static input and store it in another variable.
scnd_str = "ALL"
# Pass the given first and second strings as the arguments to the itertools.chain()
# method that combines(chains) the given two strings.
# Store it in another variable.
rslt_chain = itertools.chain(fst_str, scnd_str)
# Convert the above result into a list using the list() function and store it
# in a variable.
rslt_lst = list(rslt_chain)
# Print the result after chaining the given two strings.
print("The result after chaining the given two strings:")
print(rslt_lst)

Output:

The result after chaining the given two strings:
['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 first list as user input using list(),map(),input(),and split() functions.
  • Store it in a variable.
  • Give the second list as user input using list(),map(),input(),and split() functions.
  • Store it in another variable.
  • Pass the given first and second lists as the arguments to the itertools.chain() method that combines(chains) the given two lists.
  • Store it in another variable.
  • Convert the above result into a list using the list() function and store it in a variable.
  • Print the result after chaining the given two lists.
  • The Exit of the Program.

Below is the implementation:

# Import itertools module using the import keyword.
import itertools
# Give the first list as user input using list(),map(),input(),and split() functions.
# Store it in a variable.
fst_lst = list(map(int, input(
   'Enter some random List Elements separated by spaces = ').split()))
# Give the second list as user input using list(),map(),input(),and split() functions.
# Store it in a variable.
scnd_lst = list(map(int, input(
   'Enter some random List Elements separated by spaces = ').split()))
   
# Pass the given first and second lists as the arguments to the itertools.chain()
# method that combines(chains) the given two lists.
# Store it in another variable.
rslt_chain = itertools.chain(fst_lst, scnd_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 result after chaining the given two lists.
print("The result after chaining the given two lists:")
print(rslt_lst)

Output:

Enter some random List Elements separated by spaces = 10 15 20
Enter some random List Elements separated by spaces = 100 200 300
The result after chaining the given two lists:
[10, 15, 20, 100, 200, 300]

For Strings

Approach:

  • Import itertools module using the import keyword.
  • Give the first string as user input using the input() function and store it in a variable.
  • Give the second string as user input using the input() function and store it in another variable.
  • Pass the given first and second strings as the arguments to the itertools.chain() method that combines(chains) the given two strings.
  • Store it in another variable.
  • Convert the above result into a list using the list() function and store it in a variable.
  • Print the result after chaining the given two strings.
  • The Exit of the Program.

Below is the implementation:

# Import itertools module using the import keyword.
import itertools
# Give the first string as user input using the input() function and store it in a variable.
fst_str = input("Enter some random string = ")
# Give the second string as user input using the input() function and 
# store it in another variable.
scnd_str = input("Enter some random string = ")
# Pass the given first and second strings as the arguments to the itertools.chain()
# method that combines(chains) the given two strings.
# Store it in another variable.
rslt_chain = itertools.chain(fst_str, scnd_str)
# Convert the above result into a list using the list() function and store it
# in a variable.
rslt_lst = list(rslt_chain)
# Print the result after chaining the given two strings.
print("The result after chaining the given two strings:")
print(rslt_lst)

Output:

Enter some random string = good
Enter some random string = morning
The result after chaining the given two strings:
['g', 'o', 'o', 'd', 'm', 'o', 'r', 'n', 'i', 'n', 'g']