Combinatoric Iterators in Python - GeeksforGeeks

12 Dec, 2019
An iterator is an object that can be traversed through all its values. Simply put, iterators are data type that can be looped upon. Generators are iterators but as they cannot return values instead they yield results when they are executed, using the ‘yield’ function. Generators can be recursive just like functions. These recursive generators which are used to simplify combinatorial constructs such as permutations, combinations, and Cartesian products are called combinatoric iterators.
In python there are 4 combinatoric iterators:
Product()This tool computes the cartesian product of input iterables. 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 are tuples in sorted order.Syntax:product(iterables*, repeat=1)Examples:# import the product function from itertools modulefrom 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()Permutations() as the name speaks for itself is used to generate all possible permutations of an iterable. All elements are treated as unique based on their position and not their values. This function takes an iterable and group_size, if the value of group_size is not specified or is equal to None then the value of group_size becomes length of the iterable.Syntax:permutations(iterables*, group_size=None)Example:# import the product function from itertools modulefrom itertools import permutations  print ("All the permutations of the given list is:") print (list(permutations([1, 'geeks'], 2)))print()  print ("All the permutations of the given string is:") print (list(permutations('AB')))print()  print ("All the permutations of the given container is:") print(list(permutations(range(3), 2)))Output: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)]
Combinations():This iterator prints all the possible combinations(without replacement) of the container passed in arguments in the specified group size in sorted order.Syntax:combinations(iterables*, group_size)Examples:# import combinations from 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(2),1)))Output: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,)]

Combinations_with_replacement():This function returns a subsequence of length n from the elements of the iterable where n is the argument that the function takes determining the length of the subsequences generated by the function. Individual elements may repeat itself in combinations_with_replacement function.Syntax:combinations_with_replacement(iterables*, n=None)Examples:# import combinations from 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)))Output: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,)]

Product()This tool computes the cartesian product of input iterables. 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 are tuples in sorted order.Syntax:product(iterables*, repeat=1)Examples:# import the product function from itertools modulefrom 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)]

This tool computes the cartesian product of input iterables. 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 are tuples in sorted order.
Syntax:
product(iterables*, repeat=1)
Examples:
# import the product function from itertools modulefrom 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()Permutations() as the name speaks for itself is used to generate all possible permutations of an iterable. All elements are treated as unique based on their position and not their values. This function takes an iterable and group_size, if the value of group_size is not specified or is equal to None then the value of group_size becomes length of the iterable.Syntax:permutations(iterables*, group_size=None)Example:# import the product function from itertools modulefrom itertools import permutations  print ("All the permutations of the given list is:") print (list(permutations([1, 'geeks'], 2)))print()  print ("All the permutations of the given string is:") print (list(permutations('AB')))print()  print ("All the permutations of the given container is:") print(list(permutations(range(3), 2)))Output: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)]

Permutations() as the name speaks for itself is used to generate all possible permutations of an iterable. All elements are treated as unique based on their position and not their values. This function takes an iterable and group_size, if the value of group_size is not specified or is equal to None then the value of group_size becomes length of the iterable.
Syntax:
permutations(iterables*, group_size=None)
Example:
# import the product function from itertools modulefrom itertools import permutations  print ("All the permutations of the given list is:") print (list(permutations([1, 'geeks'], 2)))print()  print ("All the permutations of the given string is:") print (list(permutations('AB')))print()  print ("All the permutations of the given container is:") print(list(permutations(range(3), 2)))
Output:
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)]

Combinations():This iterator prints all the possible combinations(without replacement) of the container passed in arguments in the specified group size in sorted order.Syntax:combinations(iterables*, group_size)Examples:# import combinations from 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(2),1)))Output: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 iterator prints all the possible combinations(without replacement) of the container passed in arguments in the specified group size in sorted order.
Syntax:
combinations(iterables*, group_size)
Examples:
# import combinations from 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(2),1)))
Output:
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,)]


Combinations_with_replacement():This function returns a subsequence of length n from the elements of the iterable where n is the argument that the function takes determining the length of the subsequences generated by the function. Individual elements may repeat itself in combinations_with_replacement function.Syntax:combinations_with_replacement(iterables*, n=None)Examples:# import combinations from 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)))Output: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,)]

This function returns a subsequence of length n from the elements of the iterable where n is the argument that the function takes determining the length of the subsequences generated by the function. Individual elements may repeat itself in combinations_with_replacement function.
Syntax:
combinations_with_replacement(iterables*, n=None)
Examples:
# import combinations from 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)))
Output:
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-itertools
Python
Technical Scripter
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
How to Install PIP on Windows ?
Selecting rows in pandas DataFrame based on conditions
How to drop one or multiple columns in Pandas Dataframe
How To Convert Python Dictionary To JSON?
Check if element exists in list in Python
Python | Get unique values from a list
Defaultdict in Python
Python OOPs Concepts
Python | os.path.join() method
Python | Pandas dataframe.groupby()