Python

Python Permutations of a List

You might have heard or worked on permutations in Mathematics or Calculus. In the realm of mathematics, it’s a very well-known idea. The permutation is said to be a set of possible outcomes generated from a single set. Similarly, the Python language also supports permutation by utilizing some of its built-in methods and modules. Today, we will learn to get the possible permutations of a single list by using different methods in Python. Therefore, we have been utilizing the Spyder 3 tool in Windows 10 for this purpose.

Note: The article is implemented on Windows 10. The following examples can be implemented on Linux operating systems, as well.

Example 01:

Within the Spyder 3 tool, create a new Python project first. Within the newly created project, import the “itertools” module at the start of the code. After that, we have initialized an integer type list having only three elements in it. The more the elements, the more the number of permutations set. Then, we have used the itertools class object here to use the built-in method “permutations()”. This method, such as “permutations()”, has been applied to the list “L” to get permutations done for the specific list.

After getting the possible permutations of this list, the permutations have been converted into the list again and saved into a new variable, “p”. Previously, the variable “p” has been printed out as a list. The source code for this illustration is appended below:

  • import itertools
  • L = [2, 4, 6]
  • P = list(itertools.permutations(L))
  • print(p)

Let’s run this newly created code by tapping on the “Run” button from the menu bar of the Spyder 3 tool. As we have only three elements in the list, we only have only six possible sets of permutations here. The output is showing those six sets with different combinations. This code’s result is seen in the attached screenshot:

Example 02:

Within the previous example, we have all the possible permutations of a list, while every permutation has three elements determined by a compiler itself. We can also get the permutations of our choice.

For instance, we can get all the possible permutations of a list while determining the total number of elements in each permutation. So, let’s update the previous code. After importing the “itertools” module, we have initialized the same integer type list. After that, we have initialized another variable, “r”, that would be further used in the permutations() method as a parameter. It will define how many items or elements one set of permutations would be having in it.

In our instance, we have stated it as 2. Then, the same procedure has been followed to get the permutations of a list. The list “L” and variable “r” has been passed into the permutations() method as a parameter. Next, all the sets of permutations have been converted into a list and then printed out in the console via the “print” clause. The source code for this illustration is appended below:

  • import itertools
  • L = [2, 4, 6]
  • r = 2
  • P = list(itertools.permutations(l, r))
  • print(p)

When we run this code, it indicates the output as shown below. Instead of making permutations of the three elements predetermined by the compiler, it created two-element sets of permutations defined in the code. So, this is the simpler method to get permutations of your choice. This code’s result is seen in the following screenshot:

Example 03:

Let’s see another simple method to get all the possible permutations of a list. Import the “itertools” package first. Instead of initializing a list separately, we have directly passed a list to a permutations() method to get permutations. The list contains four elements. The permutations have been converted into a list and then printed out in a single line. The source code for this illustration is appended below.

  • import itertools
  • print = list(itertools.permutations(9, 5, 6, 3))

We have got a total of 24 sets of permutations for a list of four elements. This code’s result is seen in the attached screenshot.

Conclusion:

This article contains straightforward and easy-to-do examples to get the possible number of permutations for a given list data structure using simple and different methods. We are sure you will find this article useful, and it will assist you whenever you work on the Python permutations in the list.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content