I would like to generate/enumerate all possible lists of non-negative integers such that the algorithm will generate lists like the following at some point [1] [24542,0] [245,904609,848,24128,350,999] In other words, for all possible non-negative integers, generate all possible lists which contain that many non-negative integers. I have figured that the trick for a list with two numbers is to enumerate their values diagonally like this first value\second value 0 1 2 3 0 0 (this will be generated first) 2 (this third etc.) 5 9 1 1 (this second) 4 8 2 3 7 3 6 def genpair(): x = 0 y = 0 yield x,y maxx = 0 while True: maxx += 1 x = maxx y = 0 while x >= 0: yield x,y x -= 1 y += 1 gen = genpair() for i in range(10): print(next(gen)) But does the same trick (or another) also make this work for lists of arbitrary length? Continue reading...