1

If I have a list, say

a=[1,2,3,4]

I want to specify a length, let's say 3, then I can find all the subsets:

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

Does Python has a function so that I can do this quickly?

jps
  • 15,760
  • 14
  • 59
  • 71
Feng Chen
  • 1,977
  • 3
  • 26
  • 55

1 Answers1

3

You can do this:

import itertools
print(list(itertools.combinations(your_set, length_of_subsets)))
babajaj
  • 118
  • 1
  • 2
  • 10