Search code examples
pythonpython-itertoolscartesian-product

Iterating through tuple elements for product()


So I have the list of tuple as below

mylist = [(9.9, 10.0, 11.0), (19.8, 20.0, 21.0), (21.5, 22.1, 24.3)]

My problem is that I want to put each element of the list into the itertools.product() function to generate a cartesian expression.

For example, using the above list of tuple, I want it to generate as below:

itertools.product(mylist[0], mylist[1], mylist[2], .... mylist[n])

In this case, mylist[0] would be (9.9, 10.0, 11.0), mylist[1] would be (19.8, 20.0, 21.0) and so on.

How can I implement as above? Any help is greatly appreciated.


Solution

  • You can use list unpacking.

    Ex:

    from itertools import product
    
    mylist = [(9.9, 10.0, 11.0), (19.8, 20.0, 21.0), (21.5, 22.1, 24.3)]
    product(*mylist)