Getting Second-to-Last Element in List

Question:

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?

Asked By: PyNoob

||

Answers:

There is: negative indices:

lst[-2]
Answered By: Scott Hunter
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.