Answers for "python last in list"

5

last element in list py

l = [1,2,3,4,5]
last = l[len(l)-1]
Posted by: Guest on June-29-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

Python Answers by Framework

Browse Popular Code Answers by Language