-1

How can I generate all possible sublists of a given length from a list.

list=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25]    

from this, I want to generate a list of length 5 with all possible unique combinations with no dupes within sublists. sublists= [1,2,3,4,5], [2,4,5,6,7]....

Thanks Suji

Aran-Fey
  • 39,665
  • 11
  • 104
  • 149
Suji
  • 87
  • 2
  • 8

1 Answers1

1

You can do it using itertools.combinations

import itertools

l=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25] 
list(itertools.combinations(l, 5))

Also, don't use built-in such as list to name your variables.

abc
  • 11,579
  • 2
  • 26
  • 51