itertools module — Python (Part 2)

KodinGyan by Akshima Sharma
3 min readDec 1, 2020

--

Before starting this blog please check Part 1 — Itertools module. This blog cover terminating iterators on the shortest input sequence.

Topics :

accumulate()
chain()
chain.from_iterables()
compress()
dropwhile()
filterfalse()
islice()

Terminating iterators

accumulate()

accumulate function iterator takes two arguments, iterable and the function. Which would be followed at each iteration of value in iterable. If no function passed , by default addition function takes place.

   itertools.accumulate(iterable[, func]) 

Below example require itertools and operator module.

 import itertools 
import operator
li1 = [11, 24, 25, 17]
List1 = list(itertools.accumulate(li1)) # with no argument
print(List1)
List1 = list(itertools.accumulate(li1, operator.mul))
print(List1) # Multiplication Operator
Output:
[11, 35, 60, 77]
[11, 264, 6600, 112200]

Next program using max function as a function parameter, which check max value.

 import itertools
data = [5, 12, 16, 4, 25, 9, 1]
result = itertools.accumulate(data, max)
for i in result:
print(i)
Output:
5
12
16
16
25
25
25

chain()

This function takes a series of iterables and return them as one long iterable.

    itertools.chain(*iterables) import itertools
colors = [‘red’, ‘orange’, ‘yellow’, ‘green’, ‘blue’]
shapes = [‘circle’, ‘triangle’, ‘square’, ‘rectangle’]
result = itertools.chain(colors, shapes)
for each in result:
print(each)
Output :
red
orange
yellow
green
blue
circle
triangle
square
rectangle
import itertools
list1 = [1, 2, 3, 4]
list2 = [1, 5, 6, 8]
list3 = [9, 10]
list4 = list(itertools.chain(list1, list2, list3))
print(list4)
Output :
[1, 2, 3, 4, 1, 5, 6, 8, 9, 10]
import itertools
L1 = list(itertools.chain(‘ABC’, ‘DEF’, ‘GHI’, ‘JKL’))
print(L1)

chain.from_iterable()

Similar to chain() but gets chained items from a single iterable.

from itertools import chain 
data =[‘ABC’, ‘DEF’, ‘GHI’, ‘JKL’]
L1 = list(chain.from_iterable(data))
print(L1)
from itertools import chain
data =[‘123’, ‘456’, ‘78910’]
obj= map(int, list(chain.from_iterable(data)))
L1 = list(obj)
print(L1)

compress()

This function filters one iterable with another according to the bollean list values passed as selector.

itertools.compress(data, selectors)import itertools
shapes = ['circle', 'triangle', 'square', 'pentagon']
selections = [False, True, True, False]
L1 = itertools.compress(shapes, selections)
for i in L1:
print(i)

dropwhile()

itertools.dropwhile(predicate, iterable)

Make an iterator that drops elements from the iterable as long as the predicate is true; afterwards, returns every element.

data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
result = itertools.dropwhile(lambda x: x<5, data)
for i in result:
print(i)

filterfalse()

itertools.filterfalse(predicate, iterable)

This function makes an iterator that filters elements from iterable returning only those for which the predicate is False.

data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
result = itertools.filterfalse(lambda x: x<5, data)
for i in result:
print(i)
Output :
5
6
7
8
9
10

If you send None as function argument, then it will return empty list.

import itertools
data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
result = itertools.filterfalse(None, data)
for i in result:
print(i)

groupby()

itertools.groupby(iterable, key_func)

This function caluculate the keys for every element in iterable. Returns key and item group by.

islice()

itertools.islice(iterable, start, stop[, step])

This function is very much like slices. This function allows you to cut out a piece of an iterable.

colors = ['red', 'orange', 'yellow', 'green', 'blue',]
few_colors = itertools.islice(colors, 2)
for i in few_colors:
print(i)

--

--

KodinGyan by Akshima Sharma
KodinGyan by Akshima Sharma

Written by KodinGyan by Akshima Sharma

0 Followers

I am Akshima Sharma, having more than 12+ years of experience in Teaching, development, Testing and Quality.

No responses yet