def paren(left, right=None):
    if right is None:
        right = left
if left == right == 0:
    yield ''
elif left > 0:
    for p in paren(left - 1, right):
        yield '(' + p
if right > left:
    for p in paren(left, right - 1):
        yield ')' + p
How to return all valid combinations of n-pairs of parentheses?