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:
import itertools
li = [ 2 , 4 , 6 , 7 , 8 , 10 , 20 ]
iti = iter (li)
it = itertools.tee(iti, 3 )
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:
import itertools
iterator1, iterator2 = itertools.tee([ 1 , 2 , 3 , 4 , 5 , 6 , 7 ], 2 )
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:
import itertools
for i in itertools.tee([ 'a' , 'b' , 'c' , 'd' , 'e' , 'f' , 'g' ], 4 ):
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']
Last Updated :
11 Jun, 2020
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...