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

Comments & Discussion

Facing issues? Have questions? Post them here! We're happy to help!