In this example, my_list[-1] returns the last element of the list
my_list, which is 5. Negative indexing starts from the end of the list, with
-1 being the index of the last element, -2 being the index of the second-last element, and so on.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
In Python, you can get the last element of a list using negative indexing. Here's an example:
In this example, my_list[-1] returns the last element of the list my_list, which is 5. Negative indexing starts from the end of the list, with -1 being the index of the last element, -2 being the index of the second-last element, and so on.
Python supports negative indexing, where the index -1 denotes the last element, -2 denotes the second last element, and so on.
One can easily access the last element of a list as follows: