# How to use Python for Permutations and Combinations?

## Python string permutations

The permutations() function makes it simple to complete a task like discovering every possible arrangement of the letters in a Python string.

EXAMPLE

 import itertools st = "ABC" per = itertools.permutations(st) for val in per:    print(*val)

write your code here: Coding Playground

OUTPUT

 A B CA C BB A CB C AC A BC B A

The itertools object is returned by the function permutations() in exchange for a String parameter. If we attempt to print the variable "per" directly, we will obtain the results shown below:



So, in order to print each entry, a loop must be run.

## Multiple-number permutations

We must supply the numbers as a list, set, or tuple in order to find their permutations of them because the permutations() function accepts an iterable input.

EXAMPLE

 import itertools values = [1, 2, 3] per = itertools.permutations(values) for val in per:    print(*val)

write your code here: Coding Playground

OUTPUT

 1 2 31 3 22 1 32 3 13 1 23 2 1

We include all the digits or characters in the permutation calculation methods mentioned above. The permutations can have a maximum or a minimum number of elements.

## Combinations with a specific number of components

This can be done by passing an integer after the set of elements, much like the concept of "nPr," which states "Arranging r elements out of n."

EXAMPLE

 import itertools values = [1, 2, 3, 4] per = itertools.permutations(values, 2) for val in per:    print(*val)

write your code here: Coding Playground

OUTPUT

 1 21 31 42 12 32 43 13 23 44 14 24 3

The permutations() method is instructed to arrange only two elements at a time from the given list of integers in the code snippet above.

## Words with multiple letter combinations

combinations() is the function to use if you need to find all combinations that contain exactly 2 letters from a given word.

EXAMPLE

 import itertools st = "ABCDE" com = itertools.combinations(st, 2) for val in com:    print(*val)

write your code here: Coding Playground

OUTPUT

 A BA CA DA EB CB DB EC DC ED E

## Sets of numbers combined

It is possible to achieve numbers in a list, similar to the combos result we got for letters in a word.

Example

 import itertools values = [1, 2, 3, 4] com = itertools.combinations(values, 2) for val in com:    print(*val)
write your code here: Coding Playground

OUTPUT

 1 21 31 42 32 43 4

## Repetition of numerical combinations

Let's run an example of the aforementioned Note to further illustrate it.

EXAMPLE

 import itertools values = [1, 1, 2, 2] com = itertools.combinations(values, 2) for val in com:    print(*val)
write your code here: Coding Playground

OUTPUT

 1 11 21 21 21 22 2

Given that the numbers in the list are repeated, the outcome is obvious.

## Number combinations that include themselves

Combinations with replacement is a further function in the itertools package that relates to permutations and combinations (). This function is a variant of combinations(), with the minor difference that it also takes into account combinations of the elements with each other.

EXAMPLE

 import itertools values = [1, 2, 3, 4] com = itertools.combinations_with_replacement(values, 2) for val in com:    print(*val)
write your code here: Coding Playground

OUTPUT

 1 11 21 31 42 22 32 43 33 44 4

When identical arguments are supplied to the combinations() function, we can clearly distinguish between the output from the above query and the output from that query.

Numbers can be combined with one another such that more than one instance of them appears in the list. This basically occurs because the aforementioned function sets the same value when we select an element from the list in order to create combinations with other elements.

Python

### Blog | Board Infinity

At Board Infinity we have authors leading in their profession sharing their insights, ideas and inspiration. Here influential thinkers, creators, makers and doers are found in one place.