Categories
Python Answers

How to get the last element of a list with Python?

Spread the love

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

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

How to get the last element of a list with Python?

To get the last element of a list with Python, we can use index -1.

For instance, we write

some_list = [1, 2, 3]
last = some_list[-1]

to get the last item from some_list with some_list[-1].

Conclusion

To get the last element of a list with Python, we can use index -1.

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 *