Why can I use the same variable names as loop indices in nested list comprehensions?
Let's create a 5x7 matrix of zeros:
dim1 = 5
dim2 = 7
m = [[0 for _ in range(dim2)] for _ in range(dim1)]
I expect the inner comprehension to overwrite the value of variable _. Why doesn't it?
Just to be clear, the code below is supposed to be an equivalent for-loop representation. It only works as reproduced, i.e. with i and j, not with i and i as loop indices. So why does the comprehension work then?
dim1 = 5
dim2 = 7
m1 = []
for i in range(dim1):
m1.append([])
for j in range(dim2):
m1[i].append(0)