OneBite.Dev - Coding blog in a bite size

get the last element of array in python

Code snippet on how to get the last element of array in python

  last = arr[-1]

This code uses indexing to obtain the last element of an array. Indexing is a way of accessing items in an array and you can use either positive or negative numbers to index them. In this case, the negative number -1 is used, which allows the program to access the last element of the array named “arr”. After the last element is accessed, it is stored in the variable “last”. This code can be used in any program where the last element of an array needs to be accessed in order to achieve certain goals.

python