52

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?

0

10 Answers 10

50

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 
Sign up to request clarification or add additional context in comments.

3 Comments

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 The question was about the last index of the list not the last element. Therefor the way to go is len(list1)-1.
@user1270710 please read the question properly. It is about the last INDEX, not the last element.
44

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.

Comments

16

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.

Comments

13

I guess you want

last_index = len(list1) - 1 

which would store 3 in last_index.

Comments

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

2 Comments

This is super answer. This is the way to write high quality codes.
@Willysatrionugroho It could also be useless overhead. Depends on what you expect and the conditions under which the code is run.
1

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

len(list1)-1 == 3 

2 Comments

this returns a boolean
@AnthonyPerot You're right, this is a bit confusing. I think that isn't meant to be code so much as his way of saying that they are equal.
0
a = ['1', '2', '3', '4'] print len(a) - 1 3 

2 Comments

You should always add an explanation of code you are providing in an answer.
@SaschaWolf Especially when the code includes more than just the answer!
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.

Comments

-1

This might be more pythonic way:

list1.index(list1[-1])

2 Comments

I'm pretty sure this is O(N) where the other solutions are O(1), and it fails if the last item is duplicated earlier in the list.
timeit on list(range(1_000_000)) puts this solution at a runtime of 0.9161270160002459, compared to 1.918499992825673e-05 for len(list1)-1.
-4
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,

1 Comment

This will return the last element, not the last index, so it does not answer the question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.