45

Suppose I've the following list:

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

How do I obtain the last index, which in this case would be 3, of that list?

nbro
  • 15,395
  • 32
  • 113
  • 196
Sayuj
  • 7,464
  • 13
  • 59
  • 76
  • Does this answer your question? [Getting the last element of a list](https://stackoverflow.com/questions/930397/getting-the-last-element-of-a-list) – Gino Mempin Apr 13 '20 at 23:58

9 Answers9

45

len(list1)-1 is definitely the way to go, but if you absolutely need a list that has a function that returns the last index, you could create a class that inherits from list.

class MyList(list):
    def last_index(self):
        return len(self)-1


>>> l=MyList([1, 2, 33, 51])
>>> l.last_index()
3
Austin Marshall
  • 2,991
  • 16
  • 14
  • 1
    This answer is sooo bad, how did it get upvoted?? You write a whole class when list1[-1] is built into Python, much faster, and simpler? len(list1-1) is definitely NOT the way to go. – user1270710 Apr 25 '23 at 16:48
37

The best and fast way to obtain the content of the last index of a list is using -1 for number of index , for example:

my_list = [0, 1, 'test', 2, 'hi']
print(my_list[-1])

Output is: 'hi'.

Index -1 shows you the last index or first index of the end.

But if you want to get only the last index, you can obtain it with this function:

def last_index(input_list:list) -> int:
    return len(input_list) - 1

In this case, the input is the list, and the output will be an integer which is the last index number.

Ali Akhtari
  • 1,211
  • 2
  • 21
  • 42
15

Did you mean len(list1)-1?

If you're searching for other method, you can try list1.index(list1[-1]), but I don't recommend this one. You will have to be sure, that the list contains NO duplicates.

nbro
  • 15,395
  • 32
  • 113
  • 196
Gandi
  • 3,522
  • 2
  • 21
  • 31
  • Ohhh man so wise, I was going with `len(list1)-1` at first, then thought about a more Pythonic way, imagined that one, but then came here and thanks to your answer you've just saved me of a serious headache –  Aug 23 '18 at 17:30
13

I guess you want

last_index = len(list1) - 1 

which would store 3 in last_index.

nbro
  • 15,395
  • 32
  • 113
  • 196
UncleZeiv
  • 18,272
  • 7
  • 49
  • 77
2

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

Mark Anthony Libres
  • 906
  • 1
  • 7
  • 14
1

You can use the list length. The last index will be the length of the list minus one.

len(list1)-1 == 3
nbro
  • 15,395
  • 32
  • 113
  • 196
Diego Navarro
  • 9,316
  • 3
  • 26
  • 33
0
a = ['1', '2', '3', '4']
print len(a) - 1
3
Haresh Shyara
  • 1,826
  • 10
  • 13
0

Thanks for the discussion. This made me look deeper at a few assumptions and a couple of asymmetries are worth noting:

>>> l = [1,2,3,4]
>>> l
[1, 2, 3, 4]
>>> l[0:-1]
[1, 2, 3]
>>> l[0:3]
[1, 2, 3]
>>> l[0:4]
[1, 2, 3, 4]
>>> l[0:1000]
[1, 2, 3, 4]
>>> l[-1:]
[4]
  1. In indexing a list, the first index returns the first element, it also can be used to include it in a slice. Whereas the last index cannot.
  2. The last index for slicing can be any number larger than the index of the last element but the first index can only be 0.

I was debugging a function that scanned a list of data points looking for a grouping and rejecting occasional glitches. The function used a numpy windowed median filter to find the 2/3 and 1/3 transition points as indices and then it returned them. Depending on how the indices are used after the function is called could make a slight difference of 1. If the returned indeces are used to subset the data, then the last one should be:

  len(data)
instead of:
  len(data) - 1
 or:
  -1

In my gut, returning the subset list would be better because there would be no wiggle room to miss the last element. Originally, I was fixing a condition where the data ended without dropping below the 1/3 level and the first index was used as the last index. When I looked closer at the solution, I realized that the last index was always missing the last element because it returned the index to it instead of 1 past it.

strawbot
  • 371
  • 3
  • 3
-3
list1[-1]

will return the last index of your list.

If you use minus before the array index it will start counting downwards from the end. list1[-2] would return the second to last index etc.

Important to mention that -0 just returns the "first" (0th) index of the list because -0 and 0 are the same number,

Osi
  • 406
  • 3
  • 13