The Wayback Machine - https://web.archive.org/web/20240607120616/https://www.geeksforgeeks.org/python-itertools-tee/
Open In App

Python – Itertools.tee()

Last Updated : 11 Jun, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

In Python, Itertools is the inbuilt module that allows us to handle the iterators in an efficient way. They make iterating through the iterables like lists and strings very easily. One such itertools function is filterfalse().

Note: For more information, refer to Python Itertools

tee() function

This iterator splits the container into a number of iterators mentioned in the argument.

Syntax:

tee(iterator, count)

Parameter: This method contains two arguments, the first argument is iterator and the second argument is a integer.
Return Value: This method returns the number of iterators mentioned in the argument.

Example 1:




# Python code to demonstrate the working of tee() 
    
# importing "itertools" for iterator operations 
import itertools 
    
# initializing list  
li = [2, 4, 6, 7, 8, 10, 20] 
    
# storing list in iterator 
iti = iter(li) 
    
# using tee() to make a list of iterators 
# makes list of 3 iterators having same values. 
it = itertools.tee(iti, 3) 
    
# printing the values of iterators 
print ("The iterators are : ") 
for i in range (0, 3): 
    print (list(it[i])) 


Output:

The iterators are : 
[2, 4, 6, 7, 8, 10, 20]
[2, 4, 6, 7, 8, 10, 20]
[2, 4, 6, 7, 8, 10, 20]

Example 2:




# Python code to demonstrate the working of tee() 
    
# importing "itertools" for iterator operations 
import itertools
    
    
# using tee() to make a list of iterators 
  
iterator1, iterator2 = itertools.tee([1, 2, 3, 4, 5, 6, 7], 2)
    
# printing the values of iterators 
print (list(iterator1)) 
print (list(iterator1)) 
print (list(iterator2)) 


Output:

[1, 2, 3, 4, 5, 6, 7]
[]
[1, 2, 3, 4, 5, 6, 7]

Example 3:




# Python code to demonstrate the working of tee() 
    
# importing "itertools" for iterator operations 
import itertools
    
    
# using tee() to make a list of iterators 
  
for i in itertools.tee(['a', 'b', 'c', 'd', 'e', 'f', 'g'], 4):
    # printing the values of iterators 
    print (list(i))


Output:

['a', 'b', 'c', 'd', 'e', 'f', 'g']
['a', 'b', 'c', 'd', 'e', 'f', 'g']
['a', 'b', 'c', 'd', 'e', 'f', 'g']
['a', 'b', 'c', 'd', 'e', 'f', 'g']


Previous Article
Next Article

Similar Reads

Iterator Functions in Python | Set 2 (islice(), starmap(), tee()..)
Iterator Functions in Python | Set 1 1. islice(iterable, start, stop, step) :- This iterator selectively prints the values mentioned in its iterable container passed as argument. This iterator takes 4 arguments, iterable container, starting pos., ending position and step. 2. starmap(func., tuple list) :- This iterator takes a function and tuple lis
5 min read
Print first n distinct permutations of string using itertools in Python
Given a string with duplicate characters allowed, print first n permutations of given string such that no permutation is repeated. Examples: Input : string = "abcab", n = 10 Output : aabbc aabcb aacbb ababc abacb abbac abbca abcab abcba acabb Input : string = "okok", n = 4 Output : kkoo koko kook okko Approach: Python provides an inbuilt method to
2 min read
Python Itertools
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. For example, let's suppose there are two lists and you want to multiply their elements. There can be sever
13 min read
itertools.groupby() in Python
Prerequisites: Python Itertools 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. Itertools.groupby() This method calculates the keys for each element prese
2 min read
Python - itertools.repeat()
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 repeat() itertools.repeat()falls under the category
2 min read
Python - Itertools.takewhile()
The itertools is a module in Python having a collection of functions that are used for handling iterators. They make iterating through the iterables like lists and strings very easy. One such itertools function is takewhile(). Note: For more information, refer to Python Itertools takewhile() This allows considering an item from the iterable until t
3 min read
Python - Itertools.compress()
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 Compress() The itertools.compress() falls under the
2 min read
Python - Itertools.dropwhile()
Itertools is a Python module that provide various functions that work on iterators to produce complex iterators. It makes the code faster, memory efficient and thus we see a better performance. This module is either used by themselves or in combination to form iterator algebra. Note: For more information, refer to Python Itertools Dropwhile() The d
1 min read
Python - Itertools.cycle()
Iterator is defined as object types which contains values that can be accessed or iterated using a loop. There are different iterators that come built-in with Python such as lists, sets, etc. Itertools is the Python module that contains some inbuilt functions for generating sequences using iterators. This module provides various functions that work
3 min read
Python - Itertools.chain()
The itertools is a module in Python having a collection of functions that are used for handling iterators. They make iterating through the iterables like lists and strings very easily. One such itertools function is chain().Note: For more information, refer to Python Itertools chain() function It is a function that takes a series of iterables and r
4 min read
Article Tags :
Practice Tags :