1

How do you access the final item in a list without knowing how many items are in that list?

for example:

list_items = [0,0,0,0,....... ,2]
print (the last item in the list)
msvalkon
  • 11,887
  • 2
  • 42
  • 38

2 Answers2

4

I think you just mean the last index, [-1] returns the last element:

>>> print(list_items[-1])
2

Indices from the end towards the start are accessed with negative integers.

anon582847382
  • 19,907
  • 5
  • 54
  • 57
1

Use:

list_items[-1] 

to access the last element.

anon582847382
  • 19,907
  • 5
  • 54
  • 57
venpa
  • 4,268
  • 21
  • 23