how to get the last index of an array in python code example

Example 1: get last element of array python

some_list[-1]

Example 2: last element in list py

l = [1,2,3,4,5]
last = l[len(l)-1]

Example 3: python get last element of array

arr = ["cat", "dog", "rabbit"]
last_element = arr[-1]

Example 4: python last index of item in list

def list_rindex(li, x):
    for i in reversed(range(len(li))):
        if li[i] == x:
            return i
    raise ValueError("{} is not in list".format(x))

Example 5: python last element of list

>>> list[-1:] # returns indexed value
    [3]
>>> list[-1]  # returns value
    3