-1

I am currently following How to get all possible combinations of a list’s elements?. The recommended solution implements the ordered solution i.e. if you have A, B then combinations are A, B, AB.

Nevertheless, I would like to include any possible ordering of elements i.e. A, B, BA, AB. Is there any way to do that in Python?

Thank you.

Community
  • 1
  • 1
Martin
  • 79
  • 3
  • 12

1 Answers1

3

Use itertools.permutations:

import itertools

xs = 'a', 'b'
for n in range(1, len(xs)+1):
    for ys in itertools.permutations(xs, n):
        print(ys)

prints

('a',)
('b',)
('a', 'b')
('b', 'a')
falsetru
  • 357,413
  • 63
  • 732
  • 636