Negative list index

Negative numbers mean that you count from the right instead of the left. So, list[-1] refers to the last element, list[-2] is the second-last, and so on. List indexes of -x mean the xth item from the end of the list, so n[-1] means the last item in the list n .

  • A negative index accesses elements from the end of the list counting backwards.

An example to show negative index in python

>>> import array
>>> a= [1, 2, 3]
>>> print a[-3]
1
>>> print a[-2]
2
>>> print a[-1]
3
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 不强求,随万物生长的规律发展!任何事物都有最小阻力位,当你觉得最舒适的那一位,你就会发现,她确实有不一样的地方!...
    宏远_3d6b阅读 172评论 0赞 0
  • 到了烟雨蒙蒙的雨季,最难过的三月拉开了帷幕,这样的早晨唯一的渴望就是睡觉,满世界的湿冷,只有一整夜捂过的被窝是温暖...
    笑乐吗阅读 175评论 0赞 0
  • 这是一所梦想的校园 这绝对是一所值得一看的学校! 它的名字也很牛气:荣光国际学校! 这是一所民办学校,主要投资老板...
    风飞花的彩铅世界阅读 440评论 0赞 0

友情链接更多精彩内容