Take the 2-minute tour ×
list1 = [1, 2, 33, 51]

I need to check is 4 the last index of the list. just like list1.lastIndex() #=> 3 how can I do it in python?

share|improve this question
You want the last element (list1[-1]) or the length of the array (len(list1))? –  JMax Oct 25 '11 at 13:18
is there any other method except len()? –  Sayuj Oct 25 '11 at 13:20
What's wrong with len() that you want to find something else? –  Karl Knechtel Oct 25 '11 at 13:24

4 Answers 4

up vote 3 down vote accepted

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()
share|improve this answer

Did you mean len(list1)-1? What's the difference between decremented length and the index of last element?

By the way, to say properly, the index of your last element is 3, not 4. In most programming languages you count from 0, not from 1 (except fortran, matlab, lua or smalltalk for example). The elements indexes are 0, 1, 2, 3. Believe me, get used to this one. You will see why by trying to operate on list1[4]

If you're searchin 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.

share|improve this answer
One certainly does not count from zero in all programming. –  Michael J. Barber Oct 25 '11 at 13:22
To be honest, I didn't know this. You mean some specific languages or some situations? Can you give any example? –  Gandi Oct 25 '11 at 13:25
len(list1) != index of the element. That would be len(list1) - 1 –  Wipqozn Oct 25 '11 at 13:31
@Gandi LUA uses ordinals rather than offsets so it starts from one. –  agf Oct 25 '11 at 13:32
There are numerous languages that use one-based indexing, e.g., fortran, matlab, lua, smalltalk. There's also the possibility of having the index range defined when the array is declared, as was done in pascal. Zero-based indexing is the most common, IME. –  Michael J. Barber Oct 25 '11 at 13:39

I guess you want

last_index = len(your_list) - 1

because the last index in your list would be 3, not 4.

share|improve this answer

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

len(list1)-1 == 4
share|improve this answer
len(list1) - 1 == 3, not 4, for the OPs example –  Wipqozn Oct 25 '11 at 13:34

Your Answer


