# Print first n distinct permutations of string using itertools in Python

Given a string with duplicate characters allowed, print first *n *permutations of given string such that no permutation is repeated.

**Examples:**

Input :string = "abcab", n = 10Output :aabbc aabcb aacbb ababc abacb abbac abbca abcab abcba acabbInput :string = "okok", n = 4Output :kkoo koko kook okko

**Approach:**

Python provides an inbuilt method to find the permutations of any given sequence which is present in the `itertools `

package. But this method doesn’t provide unique permutations. Hence to ensure that any permutation is not repeated, we use a **set** and follow the below conditions:

- If the permutation is not present in the set, print it and insert it in the set. Increment the count of number of unique permutations.
- Else, move on to the next permutation.

Below is the implementation of the above approach:

`# Python3 program to print first n unique ` `# permutations of the string using itertools ` `from` `itertools ` `import` `permutations ` ` ` `# Function to print first n unique ` `# permutation using itertools ` `def` `nPermute(string, n): ` ` ` ` ` `# Convert the string to list and sort ` ` ` `# the characters in alphabetical order ` ` ` `strList ` `=` `sorted` `(` `list` `(string)) ` ` ` ` ` `# Create an iterator ` ` ` `permList ` `=` `permutations(strList) ` ` ` ` ` `# Keep iterating until we ` ` ` `# reach nth unique permutation ` ` ` `i ` `=` `0` ` ` `permSet ` `=` `set` `() ` ` ` `tempStr ` `=` `'' ` ` ` ` ` `while` `i < n: ` ` ` `tempStr ` `=` `''.join(permList.__next__()) ` ` ` ` ` `# Insert the string in the set ` ` ` `# if it is not already included ` ` ` `# and print it out. ` ` ` `if` `tempStr ` `not` `in` `permSet: ` ` ` `permSet.add(tempStr) ` ` ` `print` `(tempStr) ` ` ` `i ` `+` `=` `1` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `string ` `=` `"ababc"` ` ` `n ` `=` `10` ` ` `nPermute(string, n) ` |

*chevron_right*

*filter_none*

**Output:**

aabbc aabcb aacbb ababc abacb abbac abbca abcab abcba acabb

## Recommended Posts:

- Print all distinct permutations of a given string with duplicates
- Java Program to print distinct permutations of a string
- Distinct permutations of the string | Set 2
- Print distinct sorted permutations with duplicates allowed in input
- itertools.combinations() module in Python to print all possible combinations
- Iterative program to generate distinct Permutations of a String
- Write a program to print all permutations of a given string
- Print all the palindromic permutations of given string in alphabetic order
- Python | All Permutations of a string in lexicographical order without using recursion
- Program to print all distinct elements of a given integer array in Python | Ordered Dictionary
- Print k different sorted permutations of a given array
- Print all permutations with repetition of characters
- Print all permutations in sorted (lexicographic) order
- Iterative approach to print all permutations of an Array
- Python | Print all string combination from given numbers

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.