-1

I have a list which contain values input = [1, 2, 3] and would need all the possible combination of this values i.e. Output = [[], [1], [2], [3], [1,2], [1,3], [2, 3], [1,2,3]].

I have used below code but it is not showing exact output values.

output = permutations([1, 2, 3], 2)

for i in output:
    print(list(i))

Can someone help me with this?

1 Answers1

1

From the output you're giving, it seems you want the combinations and not the permutations.

You can iterate over all possible valid lengths (0 to 3) and create a sequence like that.

import itertools as it

list(it.chain.from_iterable(it.combinations([1, 2, 3], i) for i in range(4)))

will output:

[(), (1,), (2,), (3,), (1, 2), (1, 3), (2, 3), (1, 2, 3)]
Nathan Furnal
  • 2,236
  • 3
  • 12
  • 25