I'm trying to find a way to get the index of an item in a list in Python given its negative index, including an index of 0.
For example with a list, l of size 4:
l[0] # index 0
l[-1] # index 3
l[-2] # index 2
I have tried using
index = negative + len(l)
however this will not work when the index is 0.
The only way I have found so far involves an if/else statement.
index = 0 if negative == 0 else negative + len(l)
Is there a possible way to do this in Python without having to use an if statement?
I am trying to store the index of the item so I can access it later, but I am being given indices starting from 0 and moving backwards through the list, and would like to convert them from negative to positive.