Error in Creating a 2d array via plain for loop iteration but not if created via list comprehension

I am trying to create a 2d array by the following two methods. Now I get the 2d array when I use the list comprehension but I receive and error of Index out of bound when I use the normal for loop. Can someone please explain me as to why did this happen?

r=3
c=4
new = []
temp =[]
for i in range(r):
    for j in range(c):
        new[i][j]=0
        
print(new)

Result - IndexError: list index out of range

temp = [[0 for _ in range(c)] for _ in range(r)]
print("Temp", temp)

Result - [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]

Comments (0)