Python Itertools.tee() 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.tee() Function:

This iterator divides the container into the iterators specified in the argument.

Syntax:

tee(iterator, count)

Parameters

iterator: It is an iterator.

count: It is an integer.

Return Value:

The number of iterators specified in the argument is returned by this method.

Examples:

Example1:

Input:

Given list =  [1, 3, 5, 2, 7]
Given count = 4

Output:

The following iterators : 
[1, 3, 5, 2, 7]
[1, 3, 5, 2, 7]
[1, 3, 5, 2, 7]
[1, 3, 5, 2, 7]

Example2:

Input:

Given list = [2, 4,  8,  1,  3]
Given count = 2

Output:

The following iterators : 
[2, 4, 8, 1, 3]
[2, 4, 8, 1, 3]

Itertools.tee() 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.
  • Get the iterator of the given list using the iter() function.
  • Store it in a variable.
  • Pass the above iterator and some random count value as the arguments to the itertools.tee() function and store it in another variable.
  • Loop till the given count value using the for loop.
  • Inside the loop, print the list of iterator values of the above result.
  • 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 = [1, 3, 5, 2, 7]

# Get the iterator of the given list using the iter() function.
# Store it in a variable.
itertr = iter(gvn_lst)

# Pass the above iterator and some random count value as the arguments to the
# itertools.tee() function and store it in another variable.

# Using tee() function to create a list of iterators results in a given list of
# 4 iterators with the same values.
rslt = itertools.tee(itertr, 4)

# Loop till the given count value using the for loop.
print("The following iterators : ")
for itr in range(0, 4):
    # Print the list of iterator values of the above rslt.
    print(list(rslt[itr]))

Output:

The following iterators : 
[1, 3, 5, 2, 7]
[1, 3, 5, 2, 7]
[1, 3, 5, 2, 7]
[1, 3, 5, 2, 7]

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

Approach:

  • Import itertools module using the import keyword.
  • Give the list as user input using list(),map(),input(),and split() functions.
  • Store it in a variable.
  • Give the count value as user input using the int(input()) function and store it in another variable.
  • Get the iterator of the given list using the iter() function.
  • Store it in a variable.
  • Pass the above iterator and the given count value as the arguments to the itertools.tee() function and store it in another variable.
  • Loop till the given count value using the for loop.
  • Inside the loop, print the list of iterator values of the above result.
  • 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(),map(),input(),and split() functions.
# Store it in a variable.
gvn_lst = list(map(int, input(
   'Enter some random List Elements separated by spaces = ').split()))
# Give the count value as user input using the int(input()) function 
# and store it in a variable.
gvn_cnt = int(input("Enter some random number = "))
# Get the iterator of the given list using the iter() function.
# Store it in a variable.
itertr = iter(gvn_lst)

# Pass the above iterator and the given count value as the arguments to the
# itertools.tee() function and store it in another variable.

# Using tee() function to create a list of iterators results in a given list of
# given count number of iterators with the same values.
rslt = itertools.tee(itertr, gvn_cnt)

# Loop till the given count value using the for loop.
print("The following iterators : ")
for itr in range(0, gvn_cnt):
    # Print the list of iterator values of the above rslt.
    print(list(rslt[itr]))

Output:

Enter some random List Elements separated by spaces = 2 4 8 1 3
Enter some random number = 2
The following iterators : 
[2, 4, 8, 1, 3]
[2, 4, 8, 1, 3]