Permute In Python (Explanation With Examples)

In this article, we will talk about Permute in Python which is one of the important and useful topics, Permute refers to a process by which we can arrange a set of objects.

Aside from that, permute is a Python method in which we can select elements and set them in different ways to become more useful.

Why Permute in Python is useful?

In Python, permute is useful because it provides a direct method to find the combinations and permutations of a set of sequences.

For example, the characters which contain the value of (LOV) this permute method can transform them into different words (LOV, LVO, OLV, OVL, VLO, and VOL).

Also read: Python Match Case with Code Example

In order to use this method, you should need to import the (itertools package).

import itertools

To surely understand this topic you should have a piece of basic or deep knowledge of the following topics in Python.

  • Loop in Python
  • Strings in Python

2 process to generate permutations in Python

  • Using the recursion method
  • Using the itertools package

Permutations of a String using Recursion method in Python

Before we jump into the itertools library, we need to understand how to permute a given string using the recursion method.

Steps:

  • The first thing to do is define the base condition for the recursion. If the string length is equal to (i), we should need to join the array of letters and print the word.
  • Using (findPermutations()) method we need to swap the letters in the word from the position of (i-g) and (i-g).
  • Once the maximum length of the word is not reached the process will still be repeated, each word is kept or (stored) as a permutation and combinations of the original string to print join.

Example program:

def get_permutation(string, i=0):

    if i == len(string):   	 
        print("".join(string))

    for g in range(i, len(string)):

        words = [c for c in string]
   
        # swap
        words[i], words[g] = words[g], words[g]
   	 
        get_permutation(words, i + 1)

print(get_permutation('LOV'))

Output:

LOV
LVV
OOV
OVV
VOV
VVV
None

Permutations of a String using itertools package in Python

With the use of (predefined permutations() method) in the itertools package we can show all the number (permutations) of the letters of a given set of objects.

Syntax:

permutations('lov')

If the above command will be printed, we can get the location of an object to the itertools as follows.

<itertools.permutations object at 0x7f6e8c537f90>

In order to get all permutations of the word (LOV), we need to use for loop to iterate it. Each permutation word is visible and returns in the form of tuple and combinations with replacement method.

Parameters:

There are only two parameters in permutations() function.

  • Length (optional) – This parameter takes the length of each (permutation) you want to print. This takes all the value which is default to the object length.
  • Object – These parameters take the object that we have to find in permutations.

Example program:

from itertools import permutations

perm = permutations('LOV')
for g in perm:
    print(''.join(g))

Output:

LOV
LVO
OLV
OVL
VLO
VOL

Code explanation:

  • In the first line of the code, the permutations() method is imported from the itertools package.
  • Then we pass the (LOV) string as an input method. Some of the permutations are stored in a list of tuples in a perm.
  • Lastly, on the 4-5 lines of the code, the list of tuples is iterated and printed each (permutation) by joining the tuple of each character.

Conclusion

I hope this lesson has helped you learn a lot. Check out my previous article about Python Dotenv for more life-changing tutorials that could help you a lot.

Leave a Comment