Python create permutation from list without duplicates

t = ['pid', 'sn', 'uuid', 'host_id']
# Expected output

I want to extract single values first and then extract the combination without duplicates.

For e.g. ('pid','sn') and ('sn','pid') are same. I want only one value. I tried permutations in itertools but it is returning all the matches.


You must use combinations instead of permutations:

import itertools

t = ['pid', 'sn', 'uuid', 'host_id']
result = []
for n in range(1, len(t)+1):
    for res in itertools.combinations(t, n):


 ('pid', 'sn'),
 ('pid', 'uuid'),
 ('pid', 'host_id'),
 ('sn', 'uuid'),
 ('sn', 'host_id'),
 ('uuid', 'host_id'),
 ('pid', 'sn', 'uuid'),
 ('pid', 'sn', 'host_id'),
 ('pid', 'uuid', 'host_id'),
 ('sn', 'uuid', 'host_id'),
 ('pid', 'sn', 'uuid', 'host_id')]

Leave a Reply

Your email address will not be published. Required fields are marked *