PyNoob PyNoob - 2 months ago 6
Python Question

Getting Second-to-Last Element in List

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?

Answer

There is: negative indices:

lst[-2]