There are 4 combinatorial iterators in python:
This tool computes the Cartesian product of the input iterations. To compute the product of an iterable with itself, we use the optional repeat
keyword argument to specify the number of repetitions. The output of this function is — tuples
in sorted order.
Syntax :
product (iterables *, repeat = 1)
Examples :
# import product function from itertools module
from
itertools
import
product
print
(
"The cartesian product using repeat:"
)
print
(
list
(product ([
1
,
2
], repeat
=
2 )))
print
()
print
(
"The cartesian product of the containers:"
)
print
(
list
(product ([
' geeks'
,
'for'
,
'geeks'
],
' 2'
)))
print
()
print
(
"The cartesian product of the containers:"
)
print
(
list
(product (
' AB'
, [
3
,
4
] )))
Output:
The cartesian product using repeat: [(1, 1), (1, 2), (2, 1), (2, 2)] The cartesian product of the containers: [('geeks' , '2'), (' for', '2'), (' geeks', '2')] The cartesian product of the containers: [(' A', 3), ('A', 4), ('B', 3), (' B', 4)]
Permutations ()
since the name speaks for itself, it is used to generate all the possible permutations of the iterable. All elements are considered unique based on their position and not on their meaning. This function accepts an iterable and a group_size, if the group_size value is not specified or is None
then the group_size value becomes the length of the iterable.
Syntax :
permutations (iterables *, group_size = None)
Example :

Exit:
All the permutations of the given list is: [(1, 'geeks'), ( 'geeks', 1)] All the permutations of the given string is: [(' A', 'B'), (' B', 'A')] All the permutations of the given container is: [(0, 1), (0, 2), (1, 0), (1, 2), (2, 0), (2, 1)]
This iterator prints all possible combinations (no replacement) of the container passed in arguments, in the specified group size, in sorted order.
Syntax :
combinations (iterables *, group_size)
Examples :
# import combinations from the itertools module
from
itertools
import
combinations
print
(
"All the combination of list in sorted order (without replacement) is: "
)
print
(
list
(combinations ([
' A'
,
2
],
2
)))
print
()
print
(
"All the combination of string in sorted order (without replacement) is:"
)
print
(
list
(combinations (
'AB'
,
2
) ))
print
()
print
(
"All the combination of list in sorted order (without replacement) is:"
)
print
(
list
(combinations (
range < / code>
(
2
),
1
)))
Exit :
All the combination of list in sorted order (without replacement) is: [('A', 2)] All the combination of string in sorted order (without replacement) is: [('A',' B')] All the combination of list in sorted order (without replacement) is: [(0,), (1,)]
This function returns a subsequence of length n from the items being iterated over, where n — an argument that the function takes, specifying the length of the subsequences generated by the function. Individual elements may be repeated in the combination combination function.
Syntax :
combinations_with_replacement (iterables *, n = None)
Examples :
# import combinations from the itertools module
from
itertools
import
combinations_with_replacement
print
(
"All the combination of string in sorted order (with replacement) is:"
)
print
(
list
(combinations_ with_replacement (
"AB"
,
2
)))
print
()
print
(
"All the combination of list in sorted order (with replacement) is:"
)
print
(
list
(combinations_with_replacement ([
1
,
2
] ,
2
)))
print
()
print
(
" All the combination of container in sorted order (with replacement) is: "
)
print
(
list
(combinations_with_replacement (
range
(
2
),
1
)))
Exit:
All the combination of string in sorted order (with replacement) is: [('A',' A '), (' A', 'B'), (' B', 'B')] All the combination of list in sorted order (with replacement) is: [(1, 1), (1, 2), (2, 2)] All the combination of container in sorted order (with replacement) is: [(0,), (1,)]
Python:  The Bible  3 Manuscripts in 1 book:
12/08/2021
Taking into account the development of modern programming, especially the emerging programming languages that reflect modern practice, Numerical Programming: A Practical Guide for Scientists and...
08/08/2021
A Practical, NoNonsense Introduction to Python Development. You already know you want to learn Python, and a smarter way to learn Python 3 is to learn by doing. The Python Workshop focuses on buil...
23/09/2020
90 SPECIFIC WAYS TO WRITE BETTER PYTHON. The Python programming language has unique strengths and charms that can be hard to grasp. Many programmers familiar with other languages often approach Pyt...
23/09/2020