Search code examples
pythonpython-itertools

All possible tuples


possible_frequency = [0,1,2,3,4,5,6,7,8]
clamp_range = list(xrange(0, 51, 1))
possible_clamp_levels = int(len(clamp_range)*len(possible_frequency))
print possible_clamp_levels

I want to find a way to print all possible tuples (459) using possible_frequency and clamp_range that is, (0,0),(0,1), (0,2).....(8, 50)

Is there a package in python that would allow me to do this.

This will do

possible_tuples = []

for a in range(0, len(possible_frequency)):
    for b in range(0, len(clamp_range)):
        test = (possible_frequency[a], clamp_range[b])
        possible_tuples.append(test)
print possible_tuples

I need a more sophisticated way.


Solution

  • You can use itertools.product():

    from itertools import product
    
    list(product(possible_frequency, clamp_range))