Categories
Python Answers

How to get the last element of a Python list?

Spread the love

Sometimes, we want to get the last element of a Python list.

In this article, we’ll look at how to get the last element of a Python list.

How to get the last element of a Python list?

To get the last element of a Python list, we can use -1 as the index in the square brackets.

For instance, we write:

some_list = [1, 2, 3]
print(some_list[-1])

Then we see 3 logged since index -1 is the last element of the list.

Conclusion

To get the last element of a Python list, we can use -1 as the index in the square brackets.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *