+2 votes

Best answer

You can generate all possible combinations of elements by selecting 1...n elements from a list of n elements using Python's * itertools.combinations* module. The

Here is an example:

>>> from itertools import combinations

>>> a = [1, 2, 3, 4, 5]

>>> combs=[]

>>> for i in range(len(a)):

...v = combinations(a, i+1)

... combs.append(list(v))

...

>>> combs

[[(1,), (2,), (3,), (4,), (5,)], [(1, 2), (1, 3), (1, 4), (1, 5), (2, 3), (2, 4), (2, 5), (3, 4), (3, 5), (4, 5)], [(1, 2, 3), (1, 2, 4), (1, 2, 5), (1, 3, 4), (1, 3, 5), (1, 4, 5), (2, 3, 4), (2, 3, 5), (2, 4, 5), (3, 4, 5)], [(1, 2, 3, 4), (1, 2, 3, 5), (1, 2, 4, 5), (1, 3, 4, 5), (2, 3, 4, 5)], [(1, 2, 3, 4, 5)]]