Reputation: 7622
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?
Upvotes: 52
Views: 238490
Reputation: 391
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]
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.
Upvotes: 0
Reputation: 1297
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.
Upvotes: 43
Reputation: 448
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,
Upvotes: -4
Reputation: 1054
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
Upvotes: 2
Reputation: 18488
I guess you want
last_index = len(list1) - 1
which would store 3 in last_index
.
Upvotes: 13
Reputation: 9704
You can use the list length. The last index will be the length of the list minus one.
len(list1)-1 == 3
Upvotes: 1
Reputation: 3652
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.
Upvotes: 16
Reputation: 3107
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
Upvotes: 51