如何访问列表中的最后一项?(python)

如何访问列表中的最后一项?(python),python,list,Python,List,在不知道列表中有多少项的情况下,如何访问列表中的最后一项 例如: list_items = [0,0,0,0,....... ,2] print (the last item in the list) 我想你指的是最后一个索引,[-1]返回最后一个元素: >>> print(list_items[-1]) 2 从末尾到开头的索引都是用负整数访问的。我想你指的是最后一个索引,[-1]返回最后一个元素: >>> print(list_items[-1]) 2

在不知道列表中有多少项的情况下,如何访问列表中的最后一项

例如:

list_items = [0,0,0,0,....... ,2]
print (the last item in the list)

我想你指的是最后一个索引,
[-1]
返回最后一个元素:

>>> print(list_items[-1])
2

从末尾到开头的索引都是用负整数访问的。

我想你指的是最后一个索引,
[-1]
返回最后一个元素:

>>> print(list_items[-1])
2
从末尾到开头的索引使用负整数访问。

使用:

list_items[-1] 
要访问最后一个元素,请使用:

list_items[-1] 
访问最后一个元素