Python Itertools-Functions that create Iterators for efficient looping.
This module standardizes a core set of fast, memory efficient tools that are useful by themselves or in combination. Together, they form an
“iterator algebra” making it possible to construct specialized tools succinctly and efficiently in pure Python.
Python itertools module is available in Python Standard Library.
Types of Iterators provided by this module:
- Infinite Iterators
- Iterators terminating on shortest input sequence
- combinatoric iterators
Return type will be Iterator object.We can iterate through the iterator by
- using next() function
- using for loop
- converting to list object using list()
Makes an iterator that returns evenly spaced values starting with number specified in argument start.By default start is 0 and step is 1.Step can be non integer values also.This functions returns an infinite iterator.
It is also used as an argument in map() and zip() function.
zip() -zip() function is a Python built-in function which allows to combine corresponding elements from multiple sequences and return zip object which is iterator of tuples.
itertools.count() can be used as sequence in zip() function.
map()-Python map() function is used to apply function on all elements in the specified iterable and returns a map object which is an iterator. We can use itertools.count() as an argument in map() function .
Makes an iterator returning elements from the iterable and saving a copy of each.When the iterable is exhausted,returns element from the saved copy.Repeats indefinitely.
Makes an iterator that returns object over again and again.Runs indefinitely unless times argument is mentioned.
Used as an argument in map() and zip() function.
It is used as an argument in map() function.
It is used as an argument in zip() function.
Iterators terminating on the shortest input sequence:
Makes an iterator that returns accumulated sum or accumulated results of other binary functions which is mentioned in func-parameter.
Usually number of elements in the output iterable matches the number of elements in the input iterable.If initial-parameter is mentioned, it gets accumulated, so the output iterable contains one element more than the input iterable.
functools.reduce()returns only final accumulated value for similar function.
initial parameter is introduced in
Python version 3.8
Makes an iterator that returns element from the first iterable until its exhausted,then proceeds to the next iterable. It will treat consecutive sequences as a single sequence.
This function takes one iterable as an input argument and returns flattened iterable containing all elements in the input iterable. All elements in the input iterable should be iterable,otherwise it will raise TypeError.
Make an iterator that filters elements from the data returning only those that have a corresponding element in selectors that evaluates to True.Stops when either data or selectors iterables has been exhausted.
Make an iterator that drops elements from the iterable as long as the predicate is True,afterwards returns every element.The iterator does not produce any output until the predicate first becomes False.
Make an iterator that returns element from the iterable as long as the predicate is True.
Make an iterator that filters elements from iterable returning only those for which the predicate is False.If predicate is None,returns items that are False.
filter() method returns an iterator which contains elements of an iterable for which function returns True.
Makes an iterator that aggregates elements from each of the iterables. If iterables are of uneven length,missing value is filled with fillvalue. Iteration continues until longest iterable is exhausted.
In zip(),iteration continues until shortest iterable is exhausted.
Make an iterator that computes the function using arguments obtained from the iterable. Used instead of map() when argument parameters are already grouped in tuples from a single iterable (the data has been “pre-zipped”).
Makes an iterator that returns selected elements from the iterable. If start is
None, then iteration starts at zero. If step is
None, then the step defaults to one.If stop is
None,then iteration continues until the iterator is exhausted, if at all; otherwise, it stops at the specified position.
islice() doesn’t support negative values for start,stop and step.
itertools.islice(iterable,stop) itertools.islice(iterable, start, stop[, step])
Return n independent iterators from a single iterable.
Make an iterator that returns consecutive keys and groups from the iterable.
key is a function computing key value for each element.If key is not specified or None,key defaults to an identity function and returns the element unchanged.
Cartesian product of input iterables.
Cartesian product definition: The product of set X and set Y is the set that contains all ordered pairs ( x, y ) for which x belongs to X and y belongs to Y.
To compute the product of an iterable with itself, specify the number of repetitions with the optional repeat keyword argument. For example, product(A, repeat=4) means the same as product(A, A, A, A).
r length permutations of elements in the iterable. If r is not specified or is None,then r defaults to the length of the iterable and all possible full-length permutations are generated.The permutation tuples are emitted in lexicographic ordering according to the order of the input iterable. So, if the input iterable is sorted, the combination tuples will be produced in sorted order.
Elements are treated as unique based on their position, not on their value. So if the input elements are unique, there will be no repeat values in each permutation.
Note:In Permutations,order of the elements matters.
r length subsequences of elements from the input iterable.
The combination tuples are emitted in lexicographic ordering according to the order of the input iterable. So, if the input iterable is sorted, the combination tuples will be produced in sorted order.
Lexicographic ordering means way of ordering of words in alphabetical order of their component letters.
Elements are treated as unique based on their position, not on their value. So if the input elements are unique, there will be no repeat values in each combination.
r length subsequences of elements from the input iterable allowing individual elements to be repeated more than once.
Note: 1. Used as an argument in map() and zip(): count(),repeat() repeat()- supply stream of constant values to map() or zip() function. count()-it will supply different values to map() or zip() function.
2.Difference between cycle() and repeat(): cycle()-iterates over the same object again and again repeat()-returns the same object again and again.
3.Difference between reduce() and itertools.accumulate(): reduce(): * It will return only the final accumulated value. * First argument should be function and second argument should be iterable. accumulate() * It will return the running accumulated value.The elements in the output iterable will be equal to elements in the input iterable,if initial value is not mentioned. * First argument should be iterable and second argument should be function.
4.Difference between filter() and itertools.compress(): * filter()function-filters the given iterable with the help of function that test each element in the iterable is True or not. * compress():filters the given iterable based on the corresponding element in the selector. Iterable containing True /False is given as selector.
5.Difference between filter() and itertools.filterfalse(): *filter():Construct an iterator from the elements of iterable for which function returns True. *filterfalse():Construct an iterator from the elements of iterable for which function returns False.
6.Difference between zip() and itertools.zip_longest(): *zip():Iteration continues until shortest iterable is exhausted. *zip_longest():Iteration continues until longest iterable is exhausted.
7.Difference between list slicing and itertools.islice(): *List slicing creates new list *islice()-Returns an iterator.We can loop through the iterator,in the way we want.
8.Difference between itertools.permutations() and itertools.combinations(): * itertools.permutations():Order of the elements does matters. *itertools.combinations():Order of the elements doesn’t matters. Both combinations and permutations doesn’t repeat values.
9.Difference between itertools.combinations() and itertools.combinations_with_replacement * combinations():Order of element doesn’t matters and doesn’t repeat values. * combinations_with_replacement():Order of element doesn’t matters and it repeats values.
10.Difference between itertools.takewhile() and itertools.dropwhile(): * takewhile():Make an iterator that returns element from the iterable as long as the predicate is True. * dropwhile():Make an iterator that drops element from the iterable as long as the predicate is True afterwards returns every element.
Make a one-time donation
Make a monthly donation
Make a yearly donation
Choose an amount
Or enter a custom amount
Your contribution is appreciated.
Your contribution is appreciated.
Your contribution is appreciated.Buy Me a CoffeeBuy Me a CoffeeBuy Me a Coffee