xxxxxxxxxx
# initializing list
test_list = [4, 5, 2, 6, 7, 8, 10]
# initializing N
N = 5
# using list slicing
# Get last N elements from list
res = test_list[-N:]
# The last N elements of list are : [2, 6, 7, 8, 10]
xxxxxxxxxx
array = ["a", "b", "c", "d"]
num_elements = 3
print(array[-num_elements:])
xxxxxxxxxx
>>> some_list = [1, 2, 3]
>>> some_list[-1] = 5 # Set the last element
>>> some_list[-2] = 3 # Set the second to last element
>>> some_list
[1, 3, 5]