Search code examples
pythonpython-itertools

Python: faster alternative for itertools.product()?


I'm trying to find all possible combinations of a list with length = 22 & element values = 1-9.

When I use [i for i in itertools.product(range(1, 10), repeat=22)], Python crashes. Does Python have a faster alternative?


Solution

  • As everyone commented, try using the generator directly instead of using a list. finding all combinations is unclear. If you need to print them, do this:

    for i in itertools.product(range(1, 10), repeat=22):
        ... #Don't actually print, that may block your computer for a long time.
    

    if you need to do something on those values, then tell us what you need.