-1

Here is my code:

point = (3,6,9) # let all the elements of the tuple be none negative integers


def foo(point):
    for i in range(point[0]+1):
        for j in range(point[1]+1):
            for k in range(point[2]+1):
                yield (i,j,k)

My question is: What if I don't know the length of the tuple in advance? How to make function foo take any tuple as argument and do the same thing? e.g. what if point = (3,6,9,0,7) ?

du369
  • 821
  • 8
  • 22

1 Answers1

2

Use itertools.product() instead:

from itertools import product

def foo(point):
    for combo in product(*(range(i + 1) for i in point)):
        yield combo
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343