0

I am using itertools.combinations to get all possible tuples of two numbers / combinations ( [(1,2), (1,3) ...] ) of 1M numbers i.e. from [1,2,3,4, ..... , 1000000]. I can generate all combinations but is there a way to get a generator which will only return requested combinations. e.g. I want 1st 10K combinations, I can get this like

combinations = itertools.combinations(numbers, 2)
chunk = list(combinations)[:10000]

but this code will load all combinations into the memory which is a very bad idea. I want a generator that will return 1st 10K combinations without loading all combinations into memory. like this

combinations = itertools.combinations(numbers, 2)
chunk1_generator = some_magic(combinations, 0, 9999)

In a similar way,

chunk2_generator = some_magic(combinations, 10000, 19999)

Your help is most appreciated.

Zohaib Ijaz
  • 21,926
  • 7
  • 38
  • 60

0 Answers0