xxxxxxxxxx
# Get the last element in a List
list = ["A", "B", "C"]
print(list[-1])
xxxxxxxxxx
# The smart way
list = ["first item", "second item", "third item"]
print(list[len(list) - 1])
# The proper way
print(list[-1])
xxxxxxxxxx
mylist = [0, 1, 2]
mylist[-1] = 3 # sets last element
print(myList[-1]) # prints Last element
xxxxxxxxxx
number_list = [1, 2, 3]
print(number_list[-1]) #Gives 3
number_list[-1] = 5 # Set the last element
print(number_list[-1]) #Gives 5
number_list[-2] = 3 # Set the second to last element
number_list
[1, 3, 5]
xxxxxxxxxx
my_list = ["I", "Love", "Python"]
last_item_in_list = my_list[-1]
# last_item_in_list = "Python"
xxxxxxxxxx
MyList = [1, 2, 3, 4, 5]
print(MyList[len(Mylist)-1])
# Makes you look professional
xxxxxxxxxx
# Assuming the list is already defined
my_list = [1, 2, 3, 4, 5]
# Get the last element using indexing
last_element = my_list[-1]
print(last_element)