Answers for "how to get last element in list python"

1

how to print last element in a list python

lis[len(lis)-1]
Posted by: Guest on August-19-2020
1

python list last element

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
Posted by: Guest on February-16-2021

Code answers related to "how to get last element in list python"

Python Answers by Framework

Browse Popular Code Answers by Language