27rohitb.github.io

Itertools module

This is native in python (that is, available in standard library), and allows working with iterables in fast & memory efficient way.
NOTE: File objects are also iterables!, hence they can be used with these itertools!!

It contains commonly used iterators, and functions for combining several iterators.

Following are the functions with sample python script:

import itertools

Functions that can run indefinitely:

Functions that run only for finite number of times:

ALL the functions return an iterator

Combiantion():


iter_a = ['a','b','c','d']
# number of vals to be considered while making a combination:

num_of_val = 2 
#only a ,b at the given time

# num_of_val = 3 
# a,b,c at the same time

combinations( iter_a, num_of_vals )

Produce all possible combintation of element where the order does not matter.

Permutation():

Same as combination, except order of element in the output, now matters.

Product():

Give Catersian Product of the iterables, something like permutation with repeats.

itertools.product( iter_a, repeat=4 )

Combination with repeat:

itertools.combintations_with_replacement( iter_a, 4)

Iterable manipulation functions:

Combine different iterables into one:

itertools.chain( iter_a, iter_b , iter_c )

Slicing iterators:

Slicing function can be used to read file efficiently, since file objects are iterators

itertools.islice( iter_a, , starting_val*, stopping_val*, steps* ) # *-> optional argument

Selecting certain elements:

Input example:

iter_a = [‘a’, ‘b’, ‘c’] list_of_bool = [ True, False, True ] #or list_of_bool = [1,0,1]


* ```Filter( filter_func, iter_a)```:   
Returns an iterator with values True or False, depending upon which element satisfies filter function.   

* ```itertools.filterfalse( filter_func, iter_a ):``` Complement of builtin filter function.   

* ```itertools.dropwhile( filter_func, iter_a ):``` Returns iterable elements (without filtering) after hitting the first "True", (including the true value).   

* ```itertools.takewhile( filter_func, iter_a ):``` Comlpement of dropwhile function.   
    
* Does the given operation ( add, multiply etc) on the iterator elements and return that after each element.
```python
import operator

itertools.accumulate( iter_a, operator.mul ):

Groupby():

Key_function tells the grouby, how to group.

iter_a = list(dict()s)

# NOTE VERY IMPORTANT!!
# The above dict() should already be sorted with the respect to the key we are trying to group by.

def key_function(dict):
   return dict['some key']

final_data = itertools.groupby( iter_a, key_function )

fina_data is a iterator of tuple of type:

( <key>, <iterator of elements which had this key> ) 

Replicating iterator:

Note: DO NOT use the original iterator after replicating! ONLY use copies!!

copy1, copy2 = itertools( iter_a )