Skilled in SEO, content writing, and digital marketing. Completed several years of working in many organizations including multinational companies. I love to learn new things in life that keep me motivated.
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.
Aryan Kumar
01-May-2023In 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.
Krishnapriya Rajeev
28-Apr-2023Python 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: