1# using rindex()
2test_string = "abcabcabc"
3# using rindex()
4# to get last element occurrence
5res = test_string.rindex('c')
6# printing result
7print ("The index of last element occurrence: " + str(res))
8
9OUTPUT:
10 The index of last element occurrence: 8
1# using rindex()
2test_string = "abcabcabc"
3
4# using rindex()
5# to get last element occurrence
6res = test_string.rindex('c')
7# printing result
8print ("The index of last element occurrence: " + str(res))
9
10OUTPUT:
11 The index of last element occurrence: 8
1def list_rindex(li, x):
2 for i in reversed(range(len(li))):
3 if li[i] == x:
4 return i
5 raise ValueError("{} is not in list".format(x))
1# python code to get 2nd to last item in a list
2my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
3print(my_list[-2])