0

I need all the possible unique combinations of the elements of a list when divided into two sublists.

For example - if I have a list: [1, 2, 3] and I wanna divide it into two sublists, like the following:

[1], [2,3]

[1,2], [3]

[2], [1,3]

Now how would I find out all these unique combinations, Also, the order of the elements is neglected here.

Muhammad Nahid
  • 109
  • 1
  • 10

1 Answers1

0

You can use itertools.permutations()

from itertools import permutations

# Get all permutations of [1, 2, 3]
perm = permutations([1, 2, 3])

# Print the obtained permutations
for i in list(perm):
    print (list(i[:1]),list(i[1:]))
    print (list(i[:2]),list(i[2:]))

output:

[1] [2, 3]
[1, 2] [3]
[1] [3, 2]
[1, 3] [2]
[2] [1, 3]
[2, 1] [3]
[2] [3, 1]
[2, 3] [1]
[3] [1, 2]
[3, 1] [2]
[3] [2, 1]
[3, 2] [1]
ncica
  • 7,015
  • 1
  • 15
  • 37