I'm trying to convert a really simple for with the list comprehension, the code is: def triangolo(n): '''result = [] for x in range(1, n+1): lis = [] for y in range(1, x+1): lis +=[x+y] result.append(lis) return result''' result = [ [x+y for x in range(1, n+1)] for y in range(1, x+1) ] return result And this should be the output: [[2], [3, 4], [4, 5, 6], [5, 6, 7, 8]] The first part of the function (commented) is the common for and it works, but the list comprehension doesn't recognize the variable x in the range of the second iteration, this is the message I get: Cell In[5], line 10 in triangolo for y in range(1, x+1) ] NameError: name 'x' is not defined Does anybody know why? Continue reading...