I have a list where the length of the list is dynamic. If we look at the below example, the length of the list is 2 starts from 0. I am filling in this value in an excel sheet and it works fine when the length of the list is 6 but at times the length of the list is 2 or 3 and that point of time, I get "list index out of range" which is expected. If the list is out of range, I need to fill in 0 as the Value for rest of them. How can we do this?
counts = [2, 1] #list
df1.loc[0, 'Value'] = counts[0]
df1.loc[1, 'Value'] = counts[1]
df1.loc[2, 'Value'] = counts[2]
df1.loc[3, 'Value'] = counts[3]
df1.loc[4, 'Value'] = counts[4]
df1.loc[5, 'Value'] = counts[5]
Error:
df1.loc[2, 'Value'] = counts[2]
IndexError: list index out of range
Expected Results
df1.loc[0, 'Value'] = 2
df1.loc[1, 'Value'] = 1
df1.loc[2, 'Value'] = 0
df1.loc[3, 'Value'] = 0
df1.loc[4, 'Value'] = 0
df1.loc[5, 'Value'] = 0