Tutorial

Permutations and Combinations in Python

Published on August 4, 2022
author

Jayant Verma

Permutations and Combinations in Python

Permutations and Combinations of a set of elements are different arrangements of the elements of the set.

  • Combination is a collection of the elements where the order doesn’t matter
  • Permutation is an arrangement of a set where the order does matter.

Let’s consider a set as :

{A, B, C}

The permutations of the above set are as follows :

('A', 'B', 'C')
('A', 'C', 'B')
('B', 'A', 'C')
('B', 'C', 'A')
('C', 'A', 'B')
('C', 'B', 'A')

The combinations of the above set when two elements are taken together are :

('A', 'B')
('A', 'C')
('B', 'C')

In this tutorial, we will learn how to get the permutations and combinations of a group of elements in Python. We will look at sets of characters and numbers.

We will be using the combinations() and permutations() methods under the itertools module of Python.

Let’s get started.

Permutations of Numeric data

To use the permutations() method under itertools module we will first need to import the module.

import itertools

Now let’s define a set of numbers.

val = [1, 2, 3, 4]

Now too get the list of permutations let’s use the permutations() method.

perm_set = itertools.permutations(val)

The line of code above gives an itertools object. To print the different permutations we will iterate over this object.

for i in perm_set:
    print(i)

We get the output as :

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

The complete code for this section is given below :

import itertools
 
val = [1, 2, 3, 4]
 
perm_set = itertools.permutations(val)
 
for i in perm_set:
    print(i)

Permutations of a String

Next we will learn how to get the permutations of characters in a string.

We will be using the permutations() method, but this time we will pass a string as an argument.

import itertools
 
s = "ABC"

perm_set = itertools.permutations(s)
for val in perm_set:
    print(val)

Output :

('A', 'B', 'C')
('A', 'C', 'B')
('B', 'A', 'C')
('B', 'C', 'A')
('C', 'A', 'B')
('C', 'B', 'A')

Permutations of fixed length

We can find permutations of a set where we only take a specified number of elements in each permutation. This is similar to nPr in the field of mathematics.

The code for finding permutations of fixed length is given below:

import itertools
 
val = [1, 2, 3, 4]
 
perm_set = itertools.permutations(val,2)
 
for i in perm_set:
    print(i)

Output :

(1, 2)
(1, 3)
(1, 4)
(2, 1)
(2, 3)
(2, 4)
(3, 1)
(3, 2)
(3, 4)
(4, 1)
(4, 2)
(4, 3)

Combinations of Numeric data

Just like the method permutations(), we can use combinations(), also under itertools to get the combinations of a set.

While calling combinations() we need to pass two arguments, the set for finding combinations of and a number that signifies the length of each combination.

import itertools
 
val = [1, 2, 3, 4]
 
com_set = itertools.combinations(val, 2)
 
for i in com_set:
    print(i)

Output :

(1, 2)
(1, 3)
(1, 4)
(2, 3)
(2, 4)
(3, 4)

Combinations of a String

We can also get combinations of a string. To get the combinations of a string, use the following piece of code :

import itertools
 
s = "ABC"
 
com_set = itertools.combinations(s, 2)
 
for i in com_set:
    print(i)

Output :

('A', 'B')
('A', 'C')
('B', 'C')

Combinations with replacements

There is another method under the itertools module called combinations_with_replacement(). This method takes under consideration the combination of a number with itself as well.

Let’s see how it works.

For numeric set

import itertools
 
val = [1, 2, 3, 4]
 
com_set = itertools.combinations_with_replacement(val, 2)
 
for i in com_set:
    print(i)

Output :

(1, 1)
(1, 2)
(1, 3)
(1, 4)
(2, 2)
(2, 3)
(2, 4)
(3, 3)
(3, 4)
(4, 4)

You can see the difference in the output above and the output for the operation of a normal combination. Here we have combinations like (1,1) and (2,2) which are not there in regular combinations operation.

For a string

import itertools
 
val = "ABCD"
 
com_set = itertools.combinations_with_replacement(val, 2)
 
for i in com_set:
    print(i)

Output :

('A', 'A')
('A', 'B')
('A', 'C')
('A', 'D')
('B', 'B')
('B', 'C')
('B', 'D')
('C', 'C')
('C', 'D')
('D', 'D')

Conclusion

This tutorial was about finding permutations and combinations of a set in python. We used itertools module available in python to find the permutations and combinations.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the authors
Default avatar
Jayant Verma

author

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.