Itertools is a module in Python, it is used to iterate over data structures that can be stepped over using a for-loop. Such data structures are also known as iterables. This module works as a fast, memory-efficient tool that is used either by themselves or in combination to form iterator algebra.
Why to use ?
This module incorporates functions that utilize computational resources efficiently. Using this module also tends to enhance the readability and maintainability of the code.
grouper Recipe
The grouper() function can be found in the Recipes section of the itertools docs. The recipes are an excellent source of inspiration for ways to use itertools to your advantage.
Example
python3
# Python code to demonstrate the
# grouper Recipe
import itertools as it
# defining the grouper function
def grouper(inputs, n, fillvalue = None):
iters = [iter(inputs)] * n
return it.zip_longest(*iters, fillvalue = fillvalue)
alpha = ['g', 'e', 'e', 'k', 's', 'f', 'o',
'r', 'g', 'e', 'e', 'k', 's']
print(list(grouper(alpha, 3)))
Output :
[('g', 'e', 'e'), ('k', 's', 'f'), ('o', 'r', 'g'), ('e', 'e', 'k'), ('s', None, None)]
Brute force scenario
Brute force is a straightforward method of solving a problem that relies on sheer computing power and trying every possibility rather than advanced techniques to improve efficiency. There are different Brute force itertools function such as:
- combinations()
- combinations_with_replacement()
- permutations()
combinations()
The itertools.combinations() function takes two arguments—an iterable inputs and a positive integer n—and produces an iterator over tuples of all combinations of n elements in inputs.
Example
python3
# Python code to demonstrate combinations
import itertools as it
print(list(it.combinations([1, 2], 2)))
Output :
[(1, 2)]
combinations_with_replacement()
combinations_with_replacement() works just like combinations(), accepting an iterable inputs and a positive integer n, and returns an iterator over n-tuples of elements from inputs. The difference is that combinations_with_replacement() allows elements to be repeated in the tuples it returns.
Example
python3
# Python code to demonstrate combinations_with_replacement
import itertools as it
print(list(it.combinations_with_replacement([1, 2], 2)))
Output :
[(1, 1), (1, 2), (2, 2)]
permutations()
A permutation is a collection or a combination of objects from a set where the order or the arrangement of the chosen objects does matter. permutations() accepts a single iterable and produces all possible permutations (rearrangements) of its elements.
Example
python3
# Python code to demonstrate permutations
import itertools as it
print(list(it.permutations(['g', 'e', 'k'])))
Output :
[('g', 'e', 'k'), ('g', 'k', 'e'), ('e', 'g', 'k'), ('e', 'k', 'g'), ('k', 'g', 'e'), ('k', 'e', 'g')]
Flattening A List of Lists
Converting a list of lists (2D), into a list (1D) is called flattening. Flattening a list of lists merges all the sublists into one unified list.
Example
python3
# Python code to demonstrate flattening a list of lists
import itertools as it
list_of_lists = [[1, 2], [3, 4]]
chain_object = it.chain.from_iterable(list_of_lists)
# Return chain object with nested lists separated
# Convert to list to flatten
flattened_list = list(chain_object)
print(flattened_list)
Output :
[1, 2, 3, 4]
Similar Reads
Python Itertools
Python's Itertool is a module that provides various functions that work on iterators to produce complex iterators. This module works as a fast, memory-efficient tool that is used either by themselves or in combination to form iterator algebra. For example, let's suppose there are two lists and we wa
12 min read
Iterators in Python
An iterator in Python is an object that holds a sequence of values and provide sequential traversal through a collection of items such as lists, tuples and dictionaries. . The Python iterators object is initialized using the iter() method. It uses the next() method for iteration.__iter__(): __iter__
3 min read
Python - Itertools.count()
Python Itertools are a great way of creating complex iterators which helps in getting faster execution time and writing memory-efficient code. Itertools provide us with functions for creating infinite sequences and itertools.count() is one such function and it does exactly what it sounds like, it co
3 min read
Python - Itertools.islice()
In Python, Itertools is the inbuilt module that allows us to handle the iterators in an efficient way. They make iterating through the iterables like lists and strings very easily. One such itertools function is islice(). Note: For more information, refer to Python Itertools islice() function This i
2 min read
Python - Itertools.cycle()
Iterator is defined as object types which contains values that can be accessed or iterated using a loop. There are different iterators that come built-in with Python such as lists, sets, etc. Itertools is the Python module that contains some inbuilt functions for generating sequences using iterators
3 min read
Python - Itertools.tee()
In Python, Itertools is the inbuilt module that allows us to handle the iterators in an efficient way. They make iterating through the iterables like lists and strings very easily. One such itertools function is tee().Note: For more information, refer to Python Itertoolstee() function This iterator
2 min read
Python - Itertools.compress()
Pythonâs Itertool is a module that provides various functions that work on iterators to produce complex iterators. This module works as a fast, memory-efficient tool that is used either by themselves or in combination to form iterator algebra. Note: For more information, refer to Python Itertools Co
2 min read
Python - Itertools.filterfalse()
In Python, Itertools is the inbuilt module that allows us to handle the iterators in an efficient way. They make iterating through the iterables like lists and strings very easily. One such itertools function is filterfalse(). Note: For more information, refer to Python Itertools filterfalse() funct
2 min read
Python - itertools.repeat()
Pythonâs Itertool is a module that provides various functions that work on iterators to produce complex iterators. This module works as a fast, memory-efficient tool that is used either by themselves or in combination to form iterator algebra. Note: For more information, refer to Python Itertools re
2 min read
Python - Itertools.starmap()
The itertools is a module in Python having a collection of functions that are used for handling iterators. They make iterating through the iterables like lists and strings very easily. One such itertools function is starmap().Note: For more information, refer to Python Itertools starmap() function W
3 min read