I am trying to create a two dimensional array in python. However I am confused about the requirement for initializing an array. This question is related to How to define a two-dimensional array in Python
When I use a list comprehension as:
out = [[ i*j for j in range (Y)] for i in range (X)]
print (out)
Here I don't need to initialize any array. However in the second case:
out = []
for i in range (X):
for j in range (Y):
out[i][j] = i*j
This doesn't work without first initializing the array as:
out = [[ 0 for j in range (Y)] for i in range (X)]
I understand the need for initializing the array in the second case, but could someone please explain how does it work in the first case.