# Python Program to get the last item from the array

An array is a data structure, which is used to store collection of homogeneous data elements. And each element in the array is identified by an index value or key.

## Arrays in python

Python doesn’t have a built-in data structure to represent arrays, but it has a built-in array module for work with arrays. And also we can use the NumPy package.

An array defined by the array module is −

array('i', [1, 2, 3, 4])


A Numpy array defined by the NumPy module is −

array([1, 2, 3, 4])


Also, we can use the pythob list to represent arrays, for that we need to store homogeneous items into the list.

[1, 2, 3, 4]


In this article, we will see how to get the last item from an array. Let’s see the below input output scenarios to understand it briefly.

## Input Output Scenarios

Assume we have an array with 4 elements. And in output array the last element will be displayed.

Input array:
[1, 2, 3, 4, 5]
Output:
[5]


The value 5 is the last item of the given array.

## Indexing in python

To access the array elements, we can use the python positive or negative index values.

• Positive index − The Python sequences are zero-indexed, which means the indexing starts from 0 to n-1, so that the last element is available at total_items -1 index.

• negative indexing − The python negative indexing starts –n to -1, so that the last element available at -1 index.

## Using List

The len() function is a python inbuilt function, it is used to find the length of an object. By sending the list object to the len() function we can easily get the total number of elements are present in the list.

### Example

In this example, we will access the last element by executing the syntax lst[len(lst)-1].

# creating array
lst = [11, 12, 13, 14, 15, 16, 17, 18, 19]

print ("The original array is: ", lst)
print()

# get last element
result = lst[len(lst)-1]
print ("The last element is: ", result)


### Output

The original array is:  [11, 12, 13, 14, 15, 16, 17, 18, 19]
The last element is:  19


### Example

In this example, we will use the negative indices to get the last elements.

# creating array
lst = [11, 12, 13, 14, 15, 16, 17, 18, 19]

print ("The original array is: ", lst)
print()

# get last element
result = lst[-1]
print ("The last element is: ", result)


### Output

The original array is:  [11, 12, 13, 14, 15, 16, 17, 18, 19]
The last element is:  19


The index -1 represents the last element of the given array, and we have accessed the last element by executing the syntax lst[-1].

## Using NumPy array

Let’s use a NumPy array to access the last element of an array.

### Example

In the below examples we will use the NumPy array in place of a normal list to access the last array element.

import numpy

# creating array
numpy_array = numpy.random.randint(1, 10, 5)
print ("The original array is: ", numpy_array)
print()
numOfItems = 2

# get the last element
result = numpy_array[-1]
print ("The last element is: ", result)


### Output

The original array is:  [1 6 6 2 3]
The last element is:  3


The NumPy array also accepts the negative indices so in the above example we have accessed the last element by using the numpy_array[-1] syntax.

## Using Array module

By using array() method we can create a array object which stores the homogeneous items.

### Example

Let’s access the last item from an integer array using the negative index value.

import array

# creating array
array = array.array('i', [1, 3, 4, 8])

print ("The original array is: ", array)
print()

# get the last element
result = array[-1]
print ("The last element is: ", result)


### Output

The original array is:  array('i', [1, 3, 4, 8])
The last element is:  8


### Example

In this example, we will access the last item by using len() function.

import array

# creating array
arr = array.array('i', [1, 3, 4, 8])

print ("The original array is: ", arr)
print()

# get the last element
result = arr[len(arr)-1]
print ("The last element is: ", result)


### Output

The original array is:  array('i', [1, 3, 4, 8])
The last element is:  8


The statement arr[len(arr)-1] mean retrieving the last item from the array object.

Updated on: 29-May-2023

45 Views