How to obtain the last index of a list?

前端 未结 7 1137
野的像风
野的像风 2021-02-01 16:16

Suppose I\'ve the following list:

list1 = [1, 2, 33, 51]
                    ^
                    |
indices  0  1   2   3

How do I obtain the

7条回答
  •  悲&欢浪女
    2021-02-01 16:36

    all above answers is correct but however

    a = [];
    len(list1) - 1 # where 0 - 1 = -1
    

    to be more precisely

    a = [];
    index = len(a) - 1 if a else None;
    
    if index == None : raise Exception("Empty Array")
    

    since arrays is starting with 0

提交回复
热议问题