Getting Second-to-Last Element in List [duplicate]

心不动则不痛 提交于 2020-05-12 11:04:14

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!