You can use * -unpacking:
>>> A = [[1,2],[3,4]] >>> for ii in itertools.product(*A): ... print(ii) ... (1, 3) (1, 4) (2, 3) (2, 4)
ii will be a tuple of values. Instead of writing i,j,k , etc. You will use ii[0],ii[1],ii[2] . If you want to make qualitatively different things with these variables, depending on how many of them exist, there is no way to get around any branching, of course. But if the only differences were to include additional variables in the same type of operation, you can probably greatly simplify the code inside your loop.
source share