问题
I am able to get the second-to-last element of a list with the following:
>>> lst = ['a', 'b', 'c', 'd', 'e', 'f']
>>> print(lst[len(lst)-2])
e
Is there a better way than using print(lst[len(lst)-2]) to achieve this same result?
回答1:
There is: negative indices:
lst[-2]
来源:https://stackoverflow.com/questions/39838900/getting-second-to-last-element-in-list