Given a list of items in Python, how can I get all the possible combinations of the items?
There are several similar questions on this site, that suggest using itertools.combinations
, but that returns only a subset of what I need:
stuff = [1, 2, 3]
for L in range(0, len(stuff)+1):
for subset in itertools.combinations(stuff, L):
print(subset)
()
(1,)
(2,)
(3,)
(1, 2)
(1, 3)
(2, 3)
(1, 2, 3)
As you see, it returns only items in a strict order, not returning (2, 1)
, (3, 2)
, (3, 1)
, (2, 1, 3)
, (3, 1, 2)
, (2, 3, 1)
, and (3, 2, 1)
. Is there some workaround for that? I can't seem to come up with anything.