Python itertools module code examples
Python itertools Module Code Examples. The “Itertools Recipes” section of the “itertools” chapter of the Python library documentation contains many examples of using the module’s functions. I will not repeat the content in the documentation here, but will directly quote it. This section of the documentation is essential reading for learning Python functional programming.
The “Itertools Recipes” section of the “itertool” chapter of the Python standard library documentation is a great resource for learning. See https://docs.python.org/3/library/itertools.html#itertools-recipes.
Note that none of the examples listed here are importable functions from the itertools
module. If you want to use these examples in your own applications, you should read the code, understand the principles behind them, and then copy and modify them to use them.
The following table summarizes some examples of custom functions implemented based on the basic functions in the itertools
module.
Function Name | Parameter List | Return Result | |
---|---|---|---|
take |
(n, iterable) |
Returns the first n values of the iterable as input, as a list. Implemented based on the islice() function.
| |
tabulate |
(function, start=0) |
Returns function(0), function(1), … . Implemented using map(function, count())
| |
consume |
(iterator, n) |
Advances the iterable n steps. If n is None, the iterable is exhausted and an empty list is returned. | |
nth |
(iterable, n, default=None) |
Returns the nth value in the iterable. If no value is found, the default value is returned. Based on the islice() function.
| |
quantify |
(iterable, pred=bool) |
Applies a predicate function to each element of an iterable, returning the number of elements for which the result is “True.” This function is based on the sum() and map() functions, taking advantage of the fact that the Boolean value True is converted to 1 when converted to an integer. | |
padnone |
(iterable) |
Returns the values in the input iterable, appended with an unlimited number of None values. Mainly used to create functions similar to zip_longest() or map() .
| |
ncycles |
(iterable, n) |
Returns the value in an iterable n times | |
dotproduct |
(vec1, vec2) |
The mathematical definition of a vector dot product is: the sum of the products of the corresponding components of two vectors | |
flatten |
(listOfLists) |
Expand a nested list, linking multiple lists into one list | |
repeatfunc |
(func, times=None, *args) |
Executes the function func |
|
pairwise |
(iterable) |
s -> (s0, s1), (s1, s2), (s2, s3) |
|
grouper |
(iterable, n, fillvalue=None) |
Group the input data into fixed-length groups | |
roundrobin |
(*iterables) |
roundrobin('ABC', 'D', 'EF') --> A D E B F C |
|
partition |
(pred, iterable) |
(iterable, key=None) |
Lists all elements in order of appearance, excluding those that have appeared previously. |
unique_justseen |
(iterable, key=None) |
Lists all elements that are distinct from the previous element in order of appearance. Often used to remove duplicate elements from a sorted list. | |
iter_except |
(func, exception, first=None) |
Repeatedly executes the function func until an exception occurs. Often used for iterating until a KeyError or IndexError |