Answered by : mitrofanos-ntatidis
print(list[-1])
Source : | Last Update : Wed, 03 Jun 20
Answered by : yellowed-yacare-zmy6viqfj9bi
# The smart way
list = ["first item", "second item", "third item"]
print(list[len(list) - 1])
# The proper way
print(list[-1])
Source : | Last Update : Tue, 01 Jun 21
Answered by : busy-beaver-3i1rye8htk1t
mylist = [0, 1, 2]
mylist[-1] = 3 # sets last element
print(myList[-1]) # prints Last element
Source : | Last Update : Mon, 01 Jun 20
Answered by : inexpensive-ibis-a7frp9umptpo
list1 = ['a','b','c']
print(list1[-1])
Source : | Last Update : Mon, 13 Jul 20
Answered by : misty-millipede-p1be3zqodofj
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]
Source : https://stackoverflow.com/questions/930397/getting-the-last-element-of-a-list | Last Update : Thu, 14 May 20
Answered by : luengo
a = [1, 2, 3, 4]
print(a[-1])
#prints: 4
print(a[-2])
#prints: 3
Source : | Last Update : Sat, 23 Jan 21
Answered by : glorious-gentoo-5cxllfulsdia
lst = [2, 5 , 6, 3, 8, 9]
n = len(lst) #get the length
last_el = lst[n-1] #get the last element
print("The last element of the list is:", last_el)
Source : https://maschituts.com/get-the-last-element-of-a-list-in-python/ | Last Update : Fri, 07 Jan 22
Answered by : collins-emasi
# You can index using -1 to get the last item on the list
names = ['collins', 'emasi', 'sam']
l_item = names[-1]
# Output: sam
Source : | Last Update : Wed, 02 Mar 22
Answered by : bad-bug-bim6c5y9xge5
>>> a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
>>> a
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
>>> a[-9:]
[4, 5, 6, 7, 8, 9, 10, 11, 12]
Source : https://stackoverflow.com/questions/646644/how-to-get-last-items-of-a-list-in-python | Last Update : Wed, 10 Nov 21
Answered by : you
# 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)
Source : | Last Update : Tue, 19 Sep 23