I am writing a tool that is looking to generate all possible pairs of pairs from a list 13 chars long. Each pair must have their second element swapped. i.e. from the list ABCDEFGHIJKLM
I want to generate pairs such as AB CD
, AB CE
, or CD GK
.
I am using itertools.permutations to do this and it works:
perm_pairs = itertools.permutations(my_list, 4)
I am planning on iterating over perm_pairs
and use the pairs in a seperate program. However, as order is unimportant and the subsequent process has high time complexity, I want to increase efficiency by preventing pair inversions. i.e. if I have already had AB CD
I do not want to generate CD AB
.
itertools.combinations()
does not produce all the required pairs, but itertools.permeations()
generates 4x more pairs than are required to iterate over.
Is there a "middle way" where I can avoid generating inversions?