-3

I have a list:

list1 = [1,2,3]

and I want to dived it to two list such that:

list2 = [[], [1], [2], [3], [1, 2], [1, 3], [2, 3], [1, 2, 3]]
list3 = [[1,2,3], [2,3], [1, 3], [1, 2] ,[3], [2], [1], []]

I tried different loops but didn't get all combinations and I thought maybe there is an elegant way instead.

Assi Guri
  • 57
  • 8

1 Answers1

1

Perhaps using itertools.

import itertools

list2 = []
for i in range(4):
    for j in itertools.combinations([1,2,3], r=i):
        list2.append( list(j) )

the output

[[], [1], [2], [3], [1, 2], [1, 3], [2, 3], [1, 2, 3]]

danuzco
  • 55
  • 8