How to fix IndexError: index out of range: 0

I try to get a list of number of cycles in time history, but I could not get the list and it show me that indexerror, could you please help me to fix it in ignition 8.1, thank you
for i in range(0,len(pressure_range)):
for j in range(1,number_of_loads):
if 10i<urange[j]<10(i+1):
print Sum_w

Firstly, to paste code into the forum you need to use this button

As for the issue, I presume the square in you first line is actually meant to be [], in which case you’ve defined an empty list. You are then trying to access items from it before you’ve actually added anything to it. You add items with the append method on the list object eg
Sum_w.append("new item")

You haven’t identified which row has the error either

I’m guessing what you want is a dict, not a list, so that you can indeed do sum_w[i] = x without allocating sum_w[i] first.
You’re also accessing sum_w[i], on the same line, which you can’t, because it doesn’t exist. But python has you covered, you can use dict.get(x) instead of dict[x], and give it a second argument to return as default if the key you’re trying to access doesn’t exist. If you want to start from 0, you’d do sum_w[i] = sum_w.get(i, 0) + wcycles[i], so that the first time you try to access it you get 0 instead of an error.
The other solution would be to initialize the list before using it: sum_w = [0] * len(pressure_range)

Side note: if you find yourself using for i in range(len(iterable)), you might want to use for item in enumerate(iterable) instead. This is considered more “pythonic”. item will be a tuple containing the index AND element at this index of your iterable. You can unpack it directly : for i, element in enumerate(iterable), or for i, _ in enumerate(iterable) if you only want the index.


thank you