Find the last element in the Python list

Asked 2 years ago, Updated 2 years ago, 177 views

How can I find the last element in the Python list?

list python indexing

2022-09-22 22:36

1 Answers

listA[-1] is the shortest and Python way.

Besides that, To find the nth element in the background, listA[-n] You can write it like this.

When you want to change the last element, listA[-1] = 6 You can change it by designating it together

listA = [1,2,3,4,5]
listA[-1] = "Change"
print listA

[1, 2, 3, 4, 'change!']


2022-09-22 22:36

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.