def chainGang(steps, currentVars=None):
    thisOne = steps[0]
if currentVars is None:
    for item in thisOne():
        for gang in chainGang(steps[1:], [item]):
            yield gang
elif len(steps) == 1:
    for item in thisOne(currentVars[-1]):
        yield currentVars + [item]
else:
    for item in thisOne(currentVars[-1]):
        for gang in chainGang(steps[1:], currentVars + [item]):
            yield gang
Эквивалентная структура вложенного цикла с Itertools