In this article, MarsDevs presents you with the technical information on Itertools in Python, types of iterators, list of all functions in the itertools module.
Itertools, as the name suggests, are a collection of modules that handles iterators. They are fast, memory efficient, and increase the maintainability of code and readability.
For example, consider the following list, which contains different types of items in it, like string, float, integer, and boolean types of inputs. If we want to print items in sequence, we will have to iterate in the ‘for’ loop.
Now, to handle such iterators, there is an ‘itertools’ module in Python. This module is a collection of all those important functions that we need in order to handle such an iteration in a list. We will import these ‘itertools’ by writing before the loop or start of the code
These types of iterators are supported by the ‘itertools’ module Infinite iterators, Combinatoric iterators, and Terminating iterators. These are explained below in brief.
Printing it in a cyclic manner, indefinitely. Consider the following example
Note that for loop, you need to access the list only once. If you put ‘cycle’ function from ‘itertools’ module, then it will access the list again and again till the terminating condition is satisfied if any else its program will not be terminated.
This repeat infinitely, there is ‘times’ parameter which is optional, it controls how many times code should be executed.
Consider the following example, without the times' parameter
Now, consider the following example with ‘times’ parameter
You can notice here that if you do not use ‘times’ parameter or do not define the terminating condition, for ‘repeat’ iterators the code will not be terminated from running indefinitely.
Default start is 0 and step is 1. So, if we do not provide start and step values then it assumes default values as given. It prints values in a tabular manner. Consider the following example
iSo, if you start from 1 with a step size is 1, and it == 101 to break the loop, then it will print all natural numbers from 1 to 100.
As the name suggests, these are permutation, combinations, combinations_with_replacement, and product. These are only 4 types. These can be recursive, which is used to simplify complex constructs. These are explained as follows below in brief.
It returns all products after making all possible combinations of them. When you use a single list as input then you need to use the “repeat”’ parameter. “repeat” defines number of columns in the output table. In the case of two lists, then it returns all possible products of these lists. This is similar to the cartesian production of items of the given lists. Consider the following example
It returns all unique permutations of the iterable. Elements of the group would be unique based on position but not based on values. “group_size” is an optional parameter, if it is not present then it assumes the length of iterators as its value, just the same as in ”repeat” in the product. Consider the following example
You can note that there are unique outputs in the permutations but you have all possible products in the product example.
It will return all unique combinations for “group_size” in sorted order. Here, “group_size” is mandatory otherwise there will be an error without “group_size”. Consider the following example
So, there are groups of all combinations of size “group_size”.
It is similar to combinations but an element can be repeated more than once unlike combinations output. The output returns all possible combinations for the given “group_size”. The “group_size” is mandatory here to define the number of columns in the output table. Consider the following example
These iterators terminate. To produce output using functionality implemented in the iterators, these types of iterators are used in small inputs. There are various different types of terminating iterators are explained as following below with examples.
Here, the function argument is optional and when there is no function passed then the addition is done by default. It returns accumulated results. Consider the following example
In the above code, the default operator is addition.
It displays all values in the form of a chain. Consider the following example
It is similar to “chain” but, there is only one iterator container to pass instead of multiple. Consider the following example
It filters the value from passed iterable based on the boolean list passed as the second argument. Only True or 1 values are considered in the output. Consider the following example
It removes the sequence of elements till the function returns True. It will print all once the function returns False. Consider the following example
It will remove elements from the sequence till the given condition x<40 satisfies. Once false, then return all remaining elements from the sequence.
It prints only those values for the function return False. Consider the following example
It slices based on the given 3 arguments start, stop, step. Note that these are index positions in the lists but not actual values. Consider the following example
It computes function values based on argument function passed, like min, max etc. Consider the following example
It is the opposite of “dropwhile()”. It returns values from the iterable for the function return True. Once the function returns false, it prints all the remaining elements from the list. Consider the following example
Once the function returns false, it prints all the remaining elements from the list.
It splits the iterator into the count number of iterators. The “count” is optional here, and its default value of “count” is 2. Consider the following example
It returns values by aggregating them for every iterable. It an iterable exhausted, then it fills by “fillvalue”. The “fillvalue” is optional and its default value is None. Consider the following example