While running this code:
N=7
F=2
ar=[]
for i in range(N):
for j in range(F+1):
ar[i][j]=1
print(ar)
it's showing the error below and I don't understand why.
IndexError Traceback (most recent call last) in 4 for i in range(N): 5 for j in range(F+1): ----> 6 ar[i][j]=1 7 print(ar)
IndexError: list index out of range
The important thing I understood is: While initializing an array(in any dimension) We should give a default value to all the positons of array. Then only initialization completes. After that we can change or recieve new values to any positon of the array. The below code worked for me perfectly
N=7
F=2
#INITIALIZATION of 7 x 2 array with deafult value as 0
ar=[[0]*F for x in range(N)]
#RECEIVING NEW VALUES TO THE INITIALIZED ARRAY
for i in range(N):
for j in range(F):
ar[i][j]=int(input())
print(ar)