user27261
0
Q:

get last element of array python

l = [1, 2, 3]
l[-1]
6
mylist = [0, 1, 2]
mylist[-1] = 3 # sets last element
print(myList[-1]) # prints Last element
12
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]
8
print(list[-1])
3
arr = ["cat", "dog", "rabbit"]
last_element = arr[-1]
1
# Python3 code to demonstrate  
# to get first and last element of list 
# using list indexing 
  
# initializing list  
test_list = [1, 5, 6, 7, 4] 
  
# printing original list  
print ("The original list is : " +  str(test_list)) 
  
# using list indexing 
# to get first and last element of list 
res = [ test_list[0], test_list[-1] ]  
  
# printing result 
print ("The first and last element of list are : " +  str(res)) 
3

New to Communities?

Join the community