Getting the last element of a list

后端 未结 12 1476
一向
一向 2020-11-22 04:58

In Python, how do you get the last element of a list?

12条回答
  •  傲寒
    傲寒 (楼主)
    2020-11-22 05:59

    You can also use the code below, if you do not want to get IndexError when the list is empty.

    next(reversed(some_list), None)
    

提交回复
热议问题