Featured Post

Keyword to create root window in python

What are itertools in python

 (7)ITERTOOLS:

An iterator is an object that contains a countable number of values.
The Python itertools module is a collection of tools for handling iterators. Simply put, iterators are data types that can be used in a 'for loop'. The most common iterator in Python is the list.

1.Product:

combining elements from two lists.

for example:

from itertools import product
= [12]
= [34]
prod = product(a,b)
print(list(prod))

output = [(1,3), (1,4), (2,3), (2,4)]


To repeat the elements multiple times. 

for example:

from itertools import product
= [12]
= [34]
prod = product(a,b, repeat=2)
print(list(prod))

output = [(1, 3, 1, 3), (1, 3, 1, 4), (1, 3, 2, 3), (1, 3, 2, 4), (1, 4, 1, 3), (1, 4, 1, 4), (1, 4, 2, 3), (1, 4, 2, 4), (2, 3, 1, 3), (2, 3, 1, 4), (2, 3, 2, 3), (2, 3, 2, 4), (2, 4, 1, 3), (2, 4, 1, 4), (2, 4, 2, 3), (2, 4, 2, 4)]


2.Permutstions:

Rerturning all possible orderings of the input

for example: 

from itertools import permutations
= [1,2,3]
perm = permutations(a)
print(list(perm))

output = [(1, 2, 3), (1, 3, 2), (2, 1, 3), (2, 3, 1), (3, 1, 2), (3, 2, 1)]


Giving length 

for example:

from itertools import permutations
= [1,2,3]
perm = permutations(a, 2)
print(list(perm))

output = [(1, 2), (1, 3), (2, 1), (2, 3), (3, 1), (3, 2)]

 

3.Combinations:

Making all possible combinations with specified length.giving length is mandatory.

for example:

from itertools import combinations
= [1,2,3,4]
comb = combinations(a, 2)
print(list(comb))

output  =  [(1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)]


using Combinations_with_replacement

for example:

from itertools import combinations, combinations_with_replacement

# the second argument is mandatory and specifies the length of the output tuples.
comb = combinations([1234], 2)
print(list(comb))

comb = combinations_with_replacement([1234], 2)
print(list(comb))

output = [(1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)]


4.Accumulate: 

Make an iterator that returns accumulated sums or accumalated results of the binary function.

for example: 

from itertools import accumulate
= [1,2,3,4]
acc = accumulate(a)
print(a)
print(list(acc))

output = [1, 2, 3, 4]

[1, 3, 6, 10]


To mutiply.

for example: 

import operator
from itertools import accumulate
= [1,2,3,4]
acc = accumulate(a, func=operator.mul)
print(a)
print(list(acc))

output = [1, 2, 3, 4]

[1, 2, 6, 24]


Max comparison.

for example:

import operator
from itertools import accumulate
= [1,2,3,4]
acc = accumulate(a, func=max)
print(a)
print(list(acc))

output = [1, 2, 5, 4]

         [1, 2, 5, 5]  


5.Groupby: 

Make an iterator that returns consecutive keys and groups from the iterable. The key is a function computing a key value for each element.

for example:

from itertools import groupby 

def smaller_than_3(x):
    return x < 3

= [1234]
group_obj = groupby(a, key=smaller_than_3)

for key, value in group_obj:
    print(key, list(value))

output = True [1, 2]

False[3, 4]


6.Infinite iterators:

Count, Cycle, Repeat

The 'Count' iterator counts after the number you enter in the count().

for example:

from itertools import count, cycle, repeat

for i in count(10):
    print(i)

output = 10

11

12

  13

14....


for example:

from itertools import count, cycle, repeat

for i in count(10):
    print(i)
    if i == 15:
      break 

output = 

10

11

12

13

14

15

The iterator prints all values in sequence from the passed argument.

for example:

from itertools import count, cycle, repeat

= [123]

for i in cycle(a):
    print(i)

output = 1

  2

3

1

2

3....

This iterator prints the values repeatedly multiple times according to the passed argument.

for example:

from itertools import count, cycle, repeat

= [123]

for i in repeat(14):
    print(i)

output  = 1

  1

  1

  1

Comments