def generate_lists(target, n):
    if n <= 0:
        yield []
return
if target > n or target < -n:
    return
for element in [-1, 1]:
    for list in generate_lists(target - element, n - 1):
        yield list + [element]
Iterate over lists with a particular sum