You are looping over pairs of related values, not over two independent lists. There is no nesting, just a single loop which fetches one pair at a time.
for i in range(5):
j = i+10
print(i, j)
In a more general case, if j
is not always trivially derivable from i
, you could say
for i, j in [(0, 10), (1, 42), (2, 12)]:
print(i, j)
If i
and j
are populated from two existing lists of the same length
ilist = [0, 1, 2]
jlist = [10, 42, 12]
for i, j in zip(ilist, jlist):
print(i, j)
If i
is just the index within the j
list, you can use enumerate
:
for i, j in enumerate(range(10, 14)):
print(i, j)
If you think of nesting as dimensions, you can argue that there are rows (i
values) and columns (j
values to add);
for i in range(5):
result = list()
for j in (0, 10):
result.append(i+j)
print(*result)
So now, each i
value gets added to each j
value, and you collect and print those sums for each i
value. Because the first j
value on the first iteration of the inner loop is 0 every time, the first output value is conveniently identical to the input i
value.