What is list[-1] in Python Programming


In Python programming, one can access list elements by passing the index of the list in brackets as integer values (zero-indexed: starting from zero to access the first element of the list and so on). Unlike some other languages, Python allows the use of negative index values to access elements of the list from left-to-right.

So, when you make use of list[-1] you can access the last element of the list.

# Examples list[-1]

python_list = [1, 2, 3, 4, 5, 6, 7]
last_element = python_list[-1]
print(last_element)
Output:

7



Let's take another example with a list of string values.

Python list[-1] example
-

Facing issues? Have Questions? Post them here! I am happy to answer!


Author Info:

Rakesh (He/Him) is a seasoned developer with over 10 years of experience in web and app development, and a deep knowledge of operating systems. Author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail: rakesh@code2care.org

Copyright © Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap