Article Outline
Python example 'python code get last list item'
python code get last list item
Python code example: python code get last list item
my_list = ['red', 'blue', 'green']
# Get the last item with brute force using len
last_item = my_list[len(my_list) - 1]
# Remove the last item from the list using pop
last_item = my_list.pop()
# Get the last item using negative indices *preferred & quickest method*
last_item = my_list[-1]
# Get the last item using iterable unpacking
*_, last_item = my_list
Useful links
- Learn Python: https://pythonbasics.org
- Download Python: https://python.org
More Python: https://pythonprogramminglanguage.com