Getting the last element of a list

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

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

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

    Here is the solution for your query.

    a=["first","middle","last"] # A sample list
    print(a[0]) #prints the first item in the list because the index of the list always starts from 0.
    print(a[-1]) #prints the last item in the list.
    print(a[-2]) #prints the last second item in the list.
    

    Output:

    >>> first
    >>> last
    >>> middle
    

提交回复
热议问题