# Find all Permutations of sets in Python

In this article, we are going to learn about the mathematical concept of Permutation with the help of examples, where we will be learning how we can find out all orders that a set can have by arranging or rearranging the elements of the sets. Find all Permutations of sets in Python

### What is Permutation

In mathematics, permutation means an act of arranging all the members of a set into some sequence or order, or if the set is already ordered, rearranging (reordering) its elements, a process called permuting. The study of permutations of finite sets is a topic in the field of combinatorics.

### 1. Find all Permutations of sets in Python

We will be making use of the itertools module of Python and then we will define a set of Fruits. By using this set, we will find all the permutation of this set elements. Then in the second example, we will learn about how many permutations we can have by using n number of elements from a set.

So let us begin our examples.

```import itertools

Fruits = {'Apple','Grapes','Banana'}

# Find all permutations of Fruits elements

permute_fruits = set(itertools.permutations(Fruits))

print("All Permutations of {}".format(Fruits))

for permute in permute_fruits:
print(permute)

print ("Total permutations: ", len(permute_fruits))
```

Output

```All Permutations of {'Apple', 'Grapes', 'Banana'}
('Apple', 'Grapes', 'Banana')
('Grapes', 'Apple', 'Banana')
('Banana', 'Apple', 'Grapes')
('Grapes', 'Banana', 'Apple')
('Apple', 'Banana', 'Grapes')
('Banana', 'Grapes', 'Apple')
Total permutations:  6
```

If you see the output, we can see there are 6 permutations that are possible with the 3 values of set elements. To validate this we can use another maths function factorial() to get this value.

```from math import factorial
print("Factorial of 3 for 3 set elements:", factorial(3))
```

### 2. Selecting n items out of a set containing n+ items and permuting

This is our next example in which we are going to learn, how we can permute using n elements from a set when the set has more than n elements. In this example, we will use a set of fruits that have 4 elements, but for our permute we will see how many permutes we can get off taking 2 elements from this set.

```import itertools

Fruits = {'Apple','Grapes','Banana','Kiwi'}
Item=2

TotalItem = len(Fruits)
permute_Items = list(itertools.permutations(Fruits, Item))

print("{}-Item permutations from set {}:  ".format(Item,Fruits))

for fruits in permute_Items:
print(fruits)

print ("Total permutations: ", len(permute_Items))

```

Output:

```2-Item permutations from set {'Apple', 'Grapes', 'Banana', 'Kiwi'}:

('Banana', 'Apple')
('Banana', 'Grapes')
('Banana', 'Kiwi')
('Apple', 'Banana')
('Apple', 'Grapes')
('Apple', 'Kiwi')
('Grapes', 'Banana')
('Grapes', 'Apple')
('Grapes', 'Kiwi')
('Kiwi', 'Banana')
('Kiwi', 'Apple')
('Kiwi', 'Grapes')

Total permutations:12
```

If you see the output, we can see there are 12 permutations that are possible with the 2 values from the set of 4 elements. To validate this we can use another maths function factorial() to get this value.

```#Formula is  4!/(4-2)!  where ! means factorial.

from math import factorial
permutes = (factorial(4)/(factorial(4-2)))
print ("Total permutations: ", len(permutes))
```

Output:

```Total permutations: 12.0
```

#### Conclusion

In this article, we learn about the concept of permutation and how we can get the different permute values by using Python language. We hope , you will find it useful in your programs.