Print first n distinct permutations of string using itertools in Python Last Updated : 07 Jan, 2019 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 = 10 Output : aabbc aabcb aacbb ababc abacb abbac abbca abcab abcba acabb Input : string = "okok", n = 4 Output : 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 1== # 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) Output: aabbc aabcb aacbb ababc abacb abbac abbca abcab abcba acabb Comment More infoAdvertise with us Next Article Python | Permutation of a given string using inbuilt function R rituraj_jain Follow Improve Article Tags : Python Technical Scripter 2018 Python string-programs Practice Tags : python Similar Reads Python | Permutation of a given string using inbuilt function The task is to generate all the possible permutations of a given string. A permutation of a string is a rearrangement of its characters. For example, the permutations of "ABC" are "ABC", "ACB", "BAC", "BCA", "CAB", and "CBA". The number of permutations of a string with n unique characters is n! (fac 2 min read Python Program to print all permutations of a given string A permutation also called an "arrangement number" or "order," is a rearrangement of the elements of an ordered list S into a one-to-one correspondence with S itself. A string of length n has n! permutation. Source: Mathword(http://mathworld.wolfram.com/Permutation.html) Below are the permutations of 2 min read Itertools.Permutations() - Python The permutations() function in Python, part of the itertools module, generates all possible ordered arrangements of a given iterable (like a list, string, or tuple). Unlike combinations, where order doesn't matter, permutations consider the order of elements. It returns an iterator producing tuples, 3 min read Generate all permutation of a set in Python Generating all permutations of a set in Python involves creating every possible ordered arrangement of its elements. Since sets are unordered collections, the first step is usually converting the set to a list to maintain a consistent order. For example, given the set {1, 2, 3}, the permutations inc 3 min read itertools.combinations() Module in Python to Print All Possible Combinations When given an array of size n, one common problem is generating all possible combinations of r elements.For example:Input: arr[] = [1, 2, 3, 4], r = 2Output: [(1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)]Although, there can be a recursive solution for this problem, but in this article we'll focus 2 min read Python | All Permutations of a string in lexicographical order without using recursion Write a python program to print all the permutations of a string in lexicographical order. Examples: Input : python Output : hnopty hnopyt hnotpy hnotyp hnoypt ...... ytpnho ytpnoh ytpohn ytponh Input : xyz Output : xyz xzy yxz yzx zxy zyx Method 1: Using the default library itertools function permu 2 min read Like