Let’s say that you have a Python list with the following 5 foods:
How do we go about getting the last item?
It’s actually really easy, you can select the index item by it’s total length minus one.
Python has some really fantastic functionality around making your life easier. One of those such things is the ability to use
You can return an item by it’s index if you know this, or you can return it’s value from the place within a list.
By stipulating an index of
-1, we tell Python to return the last item; we can also return the second last item by using the index
Additional safety checking
option2 above, this is a nice and simple way to achieve our objective, but what if the
list() object is empty?