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
  • 20,041
  • 15
  • 75
  • 79
Feng Chen
  • 2,139
  • 4
  • 33
  • 62

1 Answers1

3

You can do this:

import itertools
print(list(itertools.combinations(your_set, length_of_subsets)))
babajaj
  • 168
  • 1
  • 2
  • 11