Open In App

Iterator Functions in Python | Set 1

Last Updated : 21 Jun, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Perquisite: Iterators in Python
Python in its definition also allows some interesting and useful iterator functions for efficient looping and making execution of the code faster. There are many build-in iterators in the module “itertools“. 
This module implements a number of iterator building blocks. 
Some useful Iterators : 
1. accumulate(iter, func) :- This iterator takes two arguments, iterable target and the function which would be followed at each iteration of value in target. If no function is passed, addition takes place by default.If the input iterable is empty, the output iterable will also be empty.
2. chain(iter1, iter2..) :- This function is used to print all the values in iterable targets one after another mentioned in its arguments.
 

Python3




# Python code to demonstrate the working of
# accumulate() and chain()
 
# importing "itertools" for iterator operations
import itertools
 
# importing "operator" for operator operations
import operator
 
# initializing list 1
li1 = [1, 4, 5, 7]
 
# initializing list 2
li2 = [1, 6, 5, 9]
 
# initializing list 3
li3 = [8, 10, 5, 4]
 
# using accumulate()
# prints the successive summation of elements
print ("The sum after each iteration is : ",end="")
print (list(itertools.accumulate(li1)))
 
# using accumulate()
# prints the successive multiplication of elements
print ("The product after each iteration is : ",end="")
print (list(itertools.accumulate(li1,operator.mul)))
 
# using chain() to print all elements of lists
print ("All values in mentioned chain are : ",end="")
print (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 mentioned chain are : [1, 4, 5, 7, 1, 6, 5, 9, 8, 10, 5, 4]

3. chain.from_iterable() :- This function is implemented similarly as chain() but the argument here is a list of lists or any other iterable container.
4. compress(iter, selector) :- This iterator selectively picks the values to print from the passed container according to the boolean list value passed as other argument. The arguments corresponding to boolean true are printed else all are skipped.
 

Python3




# Python code to demonstrate the working of
# chain.from_iterable() and compress()
 
# importing "itertools" for iterator operations
import itertools
 
# initializing list 1
li1 = [1, 4, 5, 7]
 
# initializing list 2
li2 = [1, 6, 5, 9]
 
# initializing list 3
li3 = [8, 10, 5, 4]
 
# initializing list of list
li4 = [li1, li2, li3]
 
# using chain.from_iterable() to print all elements of lists
print ("All values in mentioned chain are : ",end="")
print (list(itertools.chain.from_iterable(li4)))
 
# using compress() selectively print data values
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. dropwhile(func, seq) :- This iterator starts printing the characters only after the func. in argument returns false for the first time.
6. filterfalse(func, seq) :- As the name suggests, this iterator prints only values that return false for the passed function. 
 

Python3




# Python code to demonstrate the working of
# dropwhile() and filterfalse()
 
# importing "itertools" for iterator operations
import itertools
 
# initializing list
li = [2, 4, 5, 7, 8]
 
# using dropwhile() to start displaying after condition is false
print ("The values after condition returns false : ",end="")
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)))


Output: 
 

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

Time complexity : 

Average Case : O(N)

Amortized Case : O( N)

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

 



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
Mathematical Functions in Python | Set 1 (Numeric Functions)
In python a number of mathematical operations can be performed with ease by importing a module named "math" which defines various functions which makes our tasks easier. 1. ceil() :- This function returns the smallest integral value greater than the number. If number is already integer, same number is returned. 2. floor() :- This function returns t
3 min read
Mathematical Functions in Python | Set 2 (Logarithmic and Power Functions)
Numeric functions are discussed in set 1 below Mathematical Functions in Python | Set 1 ( Numeric Functions) Logarithmic and power functions are discussed in this set. 1. exp(a) :- This function returns the value of e raised to the power a (e**a) . 2. log(a, b) :- This function returns the logarithmic value of a with base b. If base is not mentione
3 min read
Mathematical Functions in Python | Set 3 (Trigonometric and Angular Functions)
Some of the mathematical functions are discussed in below set 1 and set 2 Mathematical Functions in Python | Set 1 (Numeric Functions) Mathematical Functions in Python | Set 2 (Logarithmic and Power Functions) Trigonometric and angular functions are discussed in this article. 1. sin() :- This function returns the sine of value passed as argument. T
3 min read
Mathematical Functions in Python | Set 4 (Special Functions and Constants)
Some of the mathematical functions are discussed in below set 1, set 2 and set 3 Mathematical Functions in Python | Set 1 (Numeric Functions) Mathematical Functions in Python | Set 2 (Logarithmic and Power Functions) Mathematical Functions in Python | Set 3 (Trigonometric and Angular Functions) Special Functions and constants are discussed in this
2 min read
Python | Difference between iterable and iterator
Iterable is an object, that one can iterate over. It generates an Iterator when passed to iter() method. An iterator is an object, which is used to iterate over an iterable object using the __next__() method. Iterators have the __next__() method, which returns the next item of the object. Note: Every iterator is also an iterable, but not every iter
3 min read
Python | range() does not return an iterator
range() : Python range function generates a list of numbers which are generally used in many situation for iteration as in for loop or in many other cases. In python range objects are not iterators. range is a class of a list of immutable objects. The iteration behavior of range is similar to iteration behavior of list in list and range we can not
2 min read
Python PIL | ImageSequence.Iterator()
PIL is the Python Imaging Library which provides the python interpreter with image editing capabilities. The ImageSequence module contains a wrapper class that lets you iterate over the frames of an image sequence. ImageSequence.Iterator() This class implements an iterator object that can be used to loop over an image sequence. You can use the [ ]
1 min read
Iterator Method - Python Design Patterns
Iterator method is a Behavioral Design Pattern that allows us to traverse the elements of the collections without taking the exposure of in-depth details of the elements. It provides a way to access the elements of complex data structure sequentially without repeating them.According to GangOfFour, Iterator Pattern is used " to access the elements o
4 min read
Ways to increment Iterator from inside the For loop in Python
For loops, in general, are used for sequential traversal. It falls under the category of definite iteration. Definite iterations mean the number of repetitions is specified explicitly in advance. But have you ever wondered, what happens, if you try to increment the value of the iterator from inside the for loop. Let's see with the help of the below
2 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg