Iterator Functions in Python | Set 1



Python`s

definition also allows for some interesting and useful iterator functions to efficiently loop and speed up code execution. The itertools module has many built-in iterators. 
This module implements a number of iterator building blocks. 
Some useful iterators:

1. accumulate (iter, func) : — this iterator takes two arguments, an iterative target and a function to follow at each iteration of the value in the target . If no function is passed, append is the default. If the input iteration is empty, the output iteration will also be empty.

2. chain (iter1, iter2 ..) : — This function is used to output all values ​​for iterable purposes one by one mentioned in its arguments.

# Python code for demonstration works
# accumulate () and chain ()

 
# import & quot; itertools & quot; for iterator operations

import itertools

 
# import & quot; operator & quot; for operator operations

import operator

 
# initializing list 1

li1 = [ 1 , 4 , 5 , 7 ]

 
# initializing list 2

li2 = [ 1 , 6 , 5 , 9 ]

  
# initialization list 3

li3 = [ 8 , 10 , 5 , 4 ]

 
# using accumulation ()
# prints a sequential sum of elements

print ( "The sum after each iteration is:" , end = " ")

print ( list (itertools.accumulate (li1)))

 

 
# using accumulation ()
# prints sequential multiplication of elements

print ( " The product after each iteration is: " , end = " ")

print ( list (itertools.accumulate (li1, operator.mul)))

 
# using chain () to print all list items

print ( "All values ​​in the mentioned chain are:" , end = "")

prin t ( list (itertools.chain (li1, li2, li3) ))

Output:

 The sum after each iteration is: [1, 5, 10, 17] The product after each iteration is: [1, 4, 20, 140] All values ​​in the mentioned chain are: [1, 4, 5, 7, 1, 6 , 5, 9, 8, 10, 5, 4] 

3. chain.from_iterable () : — this function is implemented similarly to chain (), but the argument here is a list of lists or any other iterable .

4. compress (iter, selector) : — this iterator selectively selects the values ​​to print from the given container according to the boolean list value passed as another argument. Arguments matching the boolean true are printed, otherwise all are skipped.

# Python code to demonstrate how it works
# chain.from_iterable () and compress ()

 
# import & quot; itertools & quot; for iterator operations

import itertools

 
# initializing list 1

li1 = [ 1 , 4 , 5 , 7 ]

 
# initializing list 2

li2 = [ 1 , 6 , 5 , 9 ]

  
# initialization list 3

li3 = [ 8 , 10 , 5 , 4 ]

 
# list initialization

li4 = [li1, li2, li3]

 
# using chain.from_iterable () to print all list items

print ( "All values ​​in the mentioned chain are:" , end = " ")

print ( list (itertools.chain.from_iterable (li4)))

 
# Selectively print data values ​​using compress ()

print ( " The compressed values ​​in string are: " , end = "")

print ( list (itertools.compress ( `GEEKSFORGEEKS` , [ 1 , 0 , 0 , 0 , 0 , 1 , 0 , 0 , 1 , 0 , 0 , 0 , 0 ])))

Output:

 All values ​​in mentioned chain are: [1, 4, 5 , 7, 1, 6, 5, 9, 8, 10, 5, 4] The compressed values ​​in string are: [`G`,` F`, `G`] 

5 … dropwise (func, seq) : — This iterator starts printing only characters after the function. in argument returns false the first time.

6. filterfalse (func, seq) : — As the name suggests, this iterator only prints values ​​that return false for the passed function.

Output:

 The values ​​after condition returns false: [5, 7, 8] The values ​​that return false to function are: [5 , 7] 

Link : https://docs.python.org /dev/library/itertools.html

This article courtesy of Manjit Singh . If you are as Python.Engineering and would like to contribute, you can also write an article using contribute.python.engineering or by posting an article contribute @ python.engineering. See my article appearing on the Python.Engineering homepage and help other geeks.

Please post comments if you find anything wrong or if you would like to share more information on the topic discussed above.


# Python code to demonstrate how it works
# drop while () and filterfalse ()

 
# import & quot; itertools & quot; for iterator operations

import itertools

 
# initializing list

li = [ 2 , 4 , 5 , 7 , 8 ]

 
# use drop while () to start displaying after condition is false

print ( "The values after condition returns false: " , end = " ")

< code class = "keyword"> print ( list (itertools.dropwhile ( lambda x: x % 2 = = 0 , li)))

 
# using filterfalse () to print false values ​​

print ( "The values ​​that return false to function are:" , end = "")

print ( list (itertools.filterfalse ( lambda x: x  % 2 = = 0 , li)))