Initialize The First Index Of A List In Python
I am converting something I wrote in C++ to Python. Here is a snippet of what I am trying to rewrite in python: std::vector dates(numberOfPayments.size(), 0); dates[0] =
Solution 1:
You're having this problem because you're trying to access a index that was not allocated yet.
To append things to a list you should use append
(edited to fix loop):
dates = []
dates.append(NDD_month[0])
for i in range(1, len(first_payments)):
dates.append((dates[i-1] + 12 - first_payments[i-1]) % 12)
print(dates)
Solution 2:
Since you initialized date
with []
, it is empty with a size of 0. You will need to use append()
to add elements to it.
Solution 3:
You can declare your dates
like that:
dates = [NDD_month[0]]
for i in range(len(first_payments)):
dates[i] = (dates[i-1] + 12 - first_payments[i-1]) % 12
print(dates)
Post a Comment for "Initialize The First Index Of A List In Python"