itertools is a powerful modules of python. it has a function named as permutations which prints r length of permutations of a given iterable. Here we will see python itertools permutations example.

  • Save the code as perm.py
  • Make it executable by using command ‘chmod +x perm.py’
  • Run using ./perm.py from terminal. its output would be as follows

  • The itertools function permutations do not repeat values if the string (iterable) contains unique characters.
  • The itertools function permutations do repeat values if the string (iterable)  contains duplicate characters. For example if the string is ‘abb’ then it will print ab,ab,ba,bb,ba,bb ( see below example )

Using python itertools permutations example ( remove duplicates values)

We can use python set for removing duplicate values from the output of itertools permutations function. Below example will print only unique values.