Negative Indexing in Lists in Python


Access elements from the end of a list using negative indices.

Source Code

my_list = [10, 20, 30, 40, 50]
last_item = my_list[-1]  # 50
second_last = my_list[-2]  # 40
print(f"Last item: {last_item}, Second to last: {second_last}")
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments