Iterator Functions in Python | Set 1
Last Updated :
21 Jun, 2022
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
import itertools
import operator
li1 = [ 1 , 4 , 5 , 7 ]
li2 = [ 1 , 6 , 5 , 9 ]
li3 = [ 8 , 10 , 5 , 4 ]
print ( "The sum after each iteration is : " ,end = "")
print ( list (itertools.accumulate(li1)))
print ( "The product after each iteration is : " ,end = "")
print ( list (itertools.accumulate(li1,operator.mul)))
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
import itertools
li1 = [ 1 , 4 , 5 , 7 ]
li2 = [ 1 , 6 , 5 , 9 ]
li3 = [ 8 , 10 , 5 , 4 ]
li4 = [li1, li2, li3]
print ( "All values in mentioned chain are : " ,end = "")
print ( list (itertools.chain.from_iterable(li4)))
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
import itertools
li = [ 2 , 4 , 5 , 7 , 8 ]
print ( "The values after condition returns false : " ,end = "")
print ( list (itertools.dropwhile( lambda x : x % 2 = = 0 ,li)))
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
Similar Reads
Python | Set 3 (Strings, Lists, Tuples, Iterations)
In the previous article, we read about the basics of Python. Now, we continue with some more python concepts. Strings in Python: A string is a sequence of characters that can be a combination of letters, numbers, and special characters. It can be declared in python by using single quotes, double quo
3 min read
Python String
A string is a sequence of characters. Python treats anything inside quotes as a string. This includes letters, numbers, and symbols. Python has no character data type so single character is a string of length 1. [GFGTABS] Python s = "GfG" print(s[1]) # access 2nd char s1 = s + s[0] # updat
6 min read
Python Lists
In Python, a list is a built-in dynamic sized array (automatically grows and shrinks). We can store all types of items (including another list) in a list. A list may contain mixed type of items, this is possible because a list mainly stores references at contiguous locations and actual items maybe s
6 min read
Python Tuples
A tuple in Python is an immutable ordered collection of elements. Tuples are similar to lists, but unlike lists, they cannot be changed after their creation (i.e., they are immutable). Tuples can hold elements of different data types. The main characteristics of tuples are being ordered , heterogene
7 min read
Python Sets
Python set is an unordered collection of multiple items having different datatypes. In Python, sets are mutable, unindexed and do not contain duplicates. The order of elements in a set is not preserved and can change. Creating a Set in PythonIn Python, the most basic and efficient method for creatin
11 min read
Dictionaries in Python
A Python dictionary is a data structure that stores the value in key: value pairs. Values in a dictionary can be of any data type and can be duplicated, whereas keys can't be repeated and must be immutable. Example: Here, The data is stored in key:value pairs in dictionaries, which makes it easier t
5 min read
Python Arrays
Lists in Python are the most flexible and commonly used data structure for sequential storage. They are similar to arrays in other languages but with several key differences: Dynamic Typing: Python lists can hold elements of different types in the same list. We can have an integer, a string and even
10 min read
Python If Else Statements - Conditional Statements
In Python, If-Else is a fundamental conditional statement used for decision-making in programming. If...Else statement allows to execution of specific blocks of code depending on the condition is True or False. if Statementif statement is the most simple decision-making statement. If the condition e
4 min read
Loops in Python - For, While and Nested Loops
Loops in Python are used to repeat actions efficiently. The main types are For loops (counting through items) and While loops (based on conditions). Additionally, Nested Loops allow looping within loops for more complex tasks. While all the ways provide similar basic functionality, they differ in th
10 min read
Loops and Control Statements (continue, break and pass) in Python
Python supports two types of loops: for loops and while loops. Alongside these loops, Python provides control statements like continue, break, and pass to manage the flow of the loops efficiently. This article will explore these concepts in detail. Table of Content for Loopswhile LoopsControl Statem
2 min read