In this tutorial, we will learn how to get the permutation and combination of a given data using Python. We will use Python inbuilt package to find the permutation and combination of a given number.
Permutation and combination are an essential part in mathematics. Python provides the itertools library that has the in-built functions to calculate permutation and combination.
Importing the Required library
To calculate the permutation and combination, we need to import the itertools library. We can import it using the below command.
The above statement will import the itertools library and forms a pathway to its function.
Now, we need to create the list of a sequence as an input. This list of input will return the tuple which consists of permutation and combination. We can also set the length of the permutation and combination.
A permutation is an arrangement of a set where order does matter. Python itertools module provide inbuilt permutation[] method to find the permutation. Let's understand the following example.
Example -
Output:
['1', '2', '3'] ['1', '3', '2'] ['2', '1', '3'] ['2', '3', '1'] ['3', '1', '2'] ['3', '2', '1']
In the above code, we have imported the itertools module. We called the permutation[] method which takes string as an argument and provides an itertools object. It is necessary to use for loop to get the each permutation.
Let's take two sets of permutation.
Example - 2
Output:
['A', 'B'] ['A', 'C'] ['B', 'C']
Example - 3
Output:
[1, 2, 3, 4] [1, 2, 4, 3] [1, 3, 2, 4] [1, 3, 4, 2] [1, 4, 2, 3] [1, 4, 3, 2] [2, 1, 3, 4] [2, 1, 4, 3] [2, 3, 1, 4] [2, 3, 4, 1] [2, 4, 1, 3] [2, 4, 3, 1] [3, 1, 2, 4] [3, 1, 4, 2] [3, 2, 1, 4] [3, 2, 4, 1] [3, 4, 1, 2] [3, 4, 2, 1] [4, 1, 2, 3] [4, 1, 3, 2] [4, 2, 1, 3] [4, 2, 3, 1] [4, 3, 1, 2] [4, 3, 2, 1]
In the above code, we have got the combination of the multiple integer number.
Permutation of the fixed length
We can calculate the permutation of the fixed length set where we only take a specified number of each element permutation. Let's understand the following example.
Example -
Output:
['H', 'e'] ['H', 'l'] ['H', 'l'] ['H', 'o'] ['e', 'H'] ['e', 'l'] ['e', 'l'] ['e', 'o'] ['l', 'H'] ['l', 'e'] ['l', 'l'] ['l', 'o'] ['l', 'H'] ['l', 'e'] ['l', 'l'] ['l', 'o'] ['o', 'H'] ['o', 'e'] ['o', 'l'] ['o', 'l']
In the above code, we have calculated the fixed permutation by passing length as two.
Combination of String
Combination is a collection of the element where the order doesn't matter. Python itertools module provides the combination[] method to calculate the combination of given data. We can calculate the combination of a string. Let's understand the following example.
Example -
Output:
['A', 'B'] ['A', 'C'] ['B', 'C']
Combination with Replacement
The itertools module consists of another method called combination_with_replacement[] which takes under consideration the combination of a number itself as well. Let's understand its example.
Combination of Numeric Set
Output:
['J', 'J'] ['J', 'a'] ['J', 'v'] ['J', 'a'] ['J', 't'] ['J', 'p'] ['J', 'o'] ['J', 'i'] ['J', 'n'] ['J', 't'] ['a', 'a'] ['a', 'v'] ['a', 'a'] ['a', 't'] ['a', 'p'] ['a', 'o'] ['a', 'i'] ['a', 'n'] ['a', 't'] ['v', 'v'] ['v', 'a'] ['v', 't'] ['v', 'p'] ['v', 'o'] ['v', 'i'] ['v', 'n'] ['v', 't'] ['a', 'a'] ['a', 't'] ['a', 'p'] ['a', 'o'] ['a', 'i'] ['a', 'n'] ['a', 't'] ['t', 't'] ['t', 'p'] ['t', 'o'] ['t', 'i'] ['t', 'n'] ['t', 't'] ['p', 'p'] ['p', 'o'] ['p', 'i'] ['p', 'n'] ['p', 't'] ['o', 'o'] ['o', 'i'] ['o', 'n'] ['o', 't'] ['i', 'i'] ['i', 'n'] ['i', 't'] ['n', 'n'] ['n', 't'] ['t', 't']
Combination of Numeric Set
If the given input is in the sorted order, the combination tuples will be returned in sorted order. Let's understand the following example.
Example -
Output:
[1, 1, 1] [1, 1, 2] [1, 1, 3] [1, 1, 4] [1, 2, 2] [1, 2, 3] [1, 2, 4] [1, 3, 3] [1, 3, 4] [1, 4, 4] [2, 2, 2] [2, 2, 3] [2, 2, 4] [2, 3, 3] [2, 3, 4] [2, 4, 4] [3, 3, 3] [3, 3, 4] [3, 4, 4] [4, 4, 4]
In this tutorial, we have discussed the itertools module to find the permutation and combination of the given data using the Python script.
In this tutorial, you’ll learn how to use Python to get all combinations of a list. In particular, you’ll learn how to how to use the itertool.combinations
method to generate a list of all combinations of values in a list.
The Quick Answer: Use itertools.combinations to Get All Combinations of a List
Table of Contents
What Does it Mean to Get All Combinations of a List?
In your Python journey, you may encounter the need to get all combinations of the items in a list. But what does this mean?
Let’s say you have a list that looks like this: ['a', 'b', 'c']
.
When you create a list of all possible combinations, you’ll end up with a list that looks like this: [[], ['a',], ['b',], ['c',], ['a', 'b'], ['a', 'c'], ['b', 'c'], ['a', 'b', 'c']]
. Here we get a list of tuples that contain all possible combinations without replacement.
Now that you know what it means to get a list of all possible combinations of a list in Python, let’s see how you can get this done in Python!
How to Use Itertools to Get All Combinations of a List in Python
Python comes built-in with a helpful library called itertools
, that provides helpful functions to work with iteratable objects. One of the many functions it comes with it the combinations[]
function. This, as the name implies, provides ways to generate combinations of lists.
Let’s take a look at how the combinations[]
function works:
itertools.combinations[iterable, r]
0 refers to the iterable for which you want to find combinations,from itertools import combinations sample_list = ['a', 'b', 'c'] list_combinations = list[] for n in range[len[sample_list] + 1]: list_combinations += list[combinations[sample_list, n]] print[list_combinations] # Returns: [[], ['a',], ['b',], ['c',], ['a', 'b'], ['a', 'c'], ['b', 'c'], ['a', 'b', 'c']]
1 refers to the length of the combinations you want to producefrom itertools import combinations sample_list = ['a', 'b', 'c'] list_combinations = list[] for n in range[len[sample_list] + 1]: list_combinations += list[combinations[sample_list, n]] print[list_combinations] # Returns: [[], ['a',], ['b',], ['c',], ['a', 'b'], ['a', 'c'], ['b', 'c'], ['a', 'b', 'c']]
Now that you know how the combinations[]
function works, let’s see how we can generate all possible combinations of a Python list’s items:
from itertools import combinations
sample_list = ['a', 'b', 'c']
list_combinations = list[]
for n in range[len[sample_list] + 1]:
list_combinations += list[combinations[sample_list, n]]
print[list_combinations]
# Returns: [[], ['a',], ['b',], ['c',], ['a', 'b'], ['a', 'c'], ['b', 'c'], ['a', 'b', 'c']]
Let’s break down what we’ve done here:
- We import the
3 function fromfrom itertools import combinations sample_list = ['a', 'b', 'c'] list_combinations = list[] for n in range[len[sample_list] + 1]: list_combinations += list[combinations[sample_list, n]] print[list_combinations] # Returns: [[], ['a',], ['b',], ['c',], ['a', 'b'], ['a', 'c'], ['b', 'c'], ['a', 'b', 'c']]
itertools
- We create a sample list and an empty list to store our data in
- We then create a for-loop to loop over all possible combinations of lengths. To make this dynamic, we use the
5 function, since we may not know how long our list is at any given time.from itertools import combinations sample_list = ['a', 'b', 'c'] list_combinations = list[] for n in range[len[sample_list] + 1]: list_combinations += list[combinations[sample_list, n]] print[list_combinations] # Returns: [[], ['a',], ['b',], ['c',], ['a', 'b'], ['a', 'c'], ['b', 'c'], ['a', 'b', 'c']]
- We then create a list out of the combinations object that’s returned from passing in our sample list and our
6 parameterfrom itertools import combinations sample_list = ['a', 'b', 'c'] list_combinations = list[] for n in range[len[sample_list] + 1]: list_combinations += list[combinations[sample_list, n]] print[list_combinations] # Returns: [[], ['a',], ['b',], ['c',], ['a', 'b'], ['a', 'c'], ['b', 'c'], ['a', 'b', 'c']]
We can see that our list includes a blank combination as well. If we wanted to omit this, we could change our for-loop to be from
from itertools import combinations
sample_list = ['a', 'b', 'c']
list_combinations = list[]
for n in range[len[sample_list] + 1]:
list_combinations += list[combinations[sample_list, n]]
print[list_combinations]
# Returns: [[], ['a',], ['b',], ['c',], ['a', 'b'], ['a', 'c'], ['b', 'c'], ['a', 'b', 'c']]
7, in order to have a minimum number of elements in our combination.In the next section, you’ll learn how to get all combinations of only unique values in a list.
Want to learn more about Python for-loops? Check out my in-depth tutorial here to learn all you need to know!
How to Get All Combinations of Unique Values of a List in Python
In this section, you’ll learn how to get all combinations of only unique values of a list in Python. Since Python lists can contain duplicate values, we’ll need to figure out how to do this.
Say we have a list that looks like this:
from itertools import combinations
sample_list = ['a', 'b', 'c']
list_combinations = list[]
for n in range[len[sample_list] + 1]:
list_combinations += list[combinations[sample_list, n]]
print[list_combinations]
# Returns: [[], ['a',], ['b',], ['c',], ['a', 'b'], ['a', 'c'], ['b', 'c'], ['a', 'b', 'c']]
8. Instead of including duplicate combinations of from itertools import combinations
sample_list = ['a', 'b', 'c']
list_combinations = list[]
for n in range[len[sample_list] + 1]:
list_combinations += list[combinations[sample_list, n]]
print[list_combinations]
# Returns: [[], ['a',], ['b',], ['c',], ['a', 'b'], ['a', 'c'], ['b', 'c'], ['a', 'b', 'c']]
9 in our final list, we’ll first need to remove duplicates from our list.Let’s see how we can do this in Python:
from itertools import combinations
sample_list = ['a', 'b', 'c', 'c']
list_combinations = list[]
sample_set = set[sample_list]
for n in range[len[sample_set] + 1]:
list_combinations += list[combinations[sample_set, n]]
print[list_combinations]
This follows the same logic as the example above. The only difference is that we have first created a set out of our list. Sets are a unique data structure in Python that require each item to be unique. Therefore, it’s a helpful way to de-duplicate our list.
We then iterate over the length of the set and the set itself, to create all possible combinations.
How to Get All Combinations with Replacement of a List in Python
In this final section, you’ll learn how to get all combinations of a list in Python with replacements. Meaning, that a single element has the potential for being picked again.
Let’s see how this can be done in Python, using itertools
and the
from itertools import combinations
sample_list = ['a', 'b', 'c', 'c']
list_combinations = list[]
sample_set = set[sample_list]
for n in range[len[sample_set] + 1]:
list_combinations += list[combinations[sample_set, n]]
print[list_combinations]
1 function. The function does exactly what it is described as: it gets the combinates with replacements.from itertools import combinations_with_replacement
sample_list = ['a', 'b', 'c']
list_combinations = list[]
for n in range[len[sample_list] + 1]:
list_combinations += list[combinations_with_replacement[sample_list, n]]
print[list_combinations]
# Returns: [[], ['a',], ['b',], ['c',], ['a', 'a'], ['a', 'b'], ['a', 'c'], ['b', 'b'], ['b', 'c'], ['c', 'c'], ['a', 'a', 'a'], ['a', 'a', 'b'], ['a', 'a', 'c'], ['a', 'b', 'b'], ['a', 'b', 'c'], ['a', 'c', 'c'], ['b', 'b', 'b'], ['b', 'b', 'c'], ['b', 'c', 'c'], ['c', 'c', 'c']]
We can see here that each item has the potential for being included once, twice, or three times in a list of three items.
Conclusion
In this post, you learned how to get all combinations of a list in Python. You learned how to do this with the
from itertools import combinations
sample_list = ['a', 'b', 'c', 'c']
list_combinations = list[]
sample_set = set[sample_list]
for n in range[len[sample_set] + 1]:
list_combinations += list[combinations[sample_set, n]]
print[list_combinations]
2 function and the `itertools.combinations_with_replacement_ function. The functions allow you to pass in a list and get the combinations without and with replacements, respectively.