What is the easiest way to access the last element of a list?

Question

Is there a way to find the last item in a list without using the len() function?

Answer

The last item in a list can be found using the index value computed by len() - 1 for the list. A short-hand for this is to use an index value of -1. For any list, accessing the index of [-1] will return the last element of the list without needing to use the len() function.

In the example below, both print() functions will output orange for the color.

colors = ['red', 'green', 'blue', 'yellow', 'orange']

print(colors[-1])

print(colors[len(colors) - 1])
8 Likes

I just pasted this code, I was wondering where I can learn about the function you used in the final line and its rule. I pasted the code and played around with it and can’t understand why I get syntax errors if I don’t have the ā€œ-ā€ character, which I’m assuming is a negative indices since it’s bringing up ā€œorangeā€ when printed. . I also checked len () built-in function on the Python documentation and did not see this there. Thanks!

1 Like

Hey @marctare,
I’m new to Code Academy but have done some coding before, and part of your question revolves around an idea that I learned very early on in programming - so I hope this helps!

I think your only problem is how you’re thinking about the syntax of that last line.
You said, "...can’t understand why I get syntax errors if I don’t have the '-' character, which I’m assuming is a negative indices since it’s bringing up 'orange' when printed."
And this is actually NOT true!

Let’s say you have a list,

list_one = ['cat', 'dog', 'penguin', 'beaver', 'squirrel']

list_one would have elements 1 through 5 (which would be cat, dog, penguin, beaver, squirrel) and would have indices 0 through 4 (ā€˜cat’ would be index 0, ā€˜dog’ would be index 1, etc…)

It is commonly stated that the length of a list would be n .
This is because the number of items in a list is the exact same value as the length of a list. (So n is literally just the number of elements in the list and, therefore, the length.)
Here, list_one has 5 items in it, so it’s length would be 5.

However, we have learned through the Python 3 course that indices start at 0 instead of 1. This would mean that the last index of a list would be the value (length of the list)-1 .
When looking at list_one , I stated before that the length of the list is 5 but the number of indices of the list is 4 (due to the first index being 0).
The expression (length of the list) - 1 in this case would be equivalent to 5 - 1 which would equal 4, our number of total indices in the list!

So as you can see, the statement print(colors[len(colors) - 1]) does not contain anything regarding negative indices, but it actually just subtraction!

In the example in this thread, the goal is to get the last element/item of a list.
If you don’t know how many elements are in a list but still want to access the last one OR if you want to find the last element of a list without using the len() function, then all you would have to do is name_of_list[len(name_of_list)-1]) because it will give you the last element of the list by subtracting 1 from the total length of the list, which would in turn give you the value of the index of that last element.
Then when you print, you can see the element that’s located in that index, which would be the one in the last spot of the list!

23 Likes

Wow, thanks for taking the time to explain this it was extremely helpful! :grinning_face_with_smiling_eyes:

2 Likes

yeah at first time while reading the code i thought ; len() -1 means last element but instead its like length of list - integer(1) which will give you a valid index which is at the last of the list

2 Likes