Search code examples
pythonlistpermutationpython-itertools

Limiting the number of combinations /permutations in python


I was going to generate some combination using the itertools, when i realized that as the number of elements increase the time taken will increase exponentially. Can i limit or indicate the maximum number of permutations to be produced so that itertools would stop after that limit is reached.

What i mean to say is:

Currently i have

#big_list is a list of lists
permutation_list = list(itertools.product(*big_list))

Currently this permutation list has over 6 Million permutations. I am pretty sure if i add another list, this number would hit the billion mark.

What i really need is a significant amount of permutations (lets say 5000). Is there a way to limit the size of the permutation_list that is produced?


Solution

  • You need to use itertools.islice, like this

    itertools.islice(itertools.product(*big_list), 5000)
    

    It doesn't create the entire list in memory, but it returns an iterator which consumes the actual iterable lazily. You can convert that to a list like this

    list(itertools.islice(itertools.product(*big_list), 5000))