| by Arround The Web | No comments

Python Permutations of a List

The permutations of a list simply mean finding out all the combinations (ordering-based) of the elements inside a set or a list. This practice is greatly used in mathematics and calculus. However, it is not limited to mathematics/calculus as it is also used in modern-day programming to perform data manipulations and analytics.

To do this, python has a library “itertools” which provides various functions to help the users perform various mathematical operations, including “permutations” of a set or a list.

What is the itertools.permutations() Method?

The “permutations()” method inside the itertools library is used to take a list as input and returns the permutations object as the return value. To understand this method, take a look at the following syntax:

itertools.permutations (listVar, r)

In this syntax:

  • listVar is the list variable that will be used to calculate permutations
  • r is the length of each tuple in the output

Note: It is a good practice to change the returned output into a list by applying the list() method to it. Doing this will make the output readable.

How to Find Permutations of a Numeric List in Python?

To find the permutations of a numeric list, import the itertools library and create an integer list using the following line:

import itertools

intList = [2,6,8]

After that, simply call the permutations() method and pass in the intList variable and convert the result into a List and store it inside a variable:

result = list(itertools.permutations(intList))

After that simply print the “result” variable onto the terminal using the following print statement:

print(result)

Running the above code will create the following output on the terminal:

All of the possible permutations have been printed onto the terminal

How to Find Permutations of a List With Fixed Tuple Length?

The user can put a limit on the size of each tuple by passing in length in the permutations method’s second argument. To demonstrate this, take the following code:

import itertools

intList = [2,6,8]

result = list(itertools.permutations(intList,2))

print(result)

In this code, the tuple size has been passed as “2” and running this code produces the following output:

From the output, it can be easily observed that all possible permutations have been printed out on the terminal while keeping the size of a tuple at “2”.

How to Find Permutations of a String in Python?

A string in python is also considered to be a “list”, which basically means that a string can be passed into the permutations method, and all of its individual characters will be used to create possible permutations. To demonstrate this, take the following code snippet:

import itertools

textString = "ABCD"

result = list(itertools.permutations(textString))

print(result)

Running this code snippet will produce the following permutations on the terminal:

This proves that Python can be used to find the permutations of all the characters of a string.

Conclusion

The itertools package comes by default with Python installation. This package contains the permutations() method that can be used to calculate all the possible permutations of a list in python. The list can be of any type (integer, string, characters). This post has gone over the syntax and the usage of the permutations() method from the itertools package.

Share Button

Source: linuxhint.com

Leave a Reply