Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python Program to get the last item from the array
An array is a data structure used to store a collection of homogeneous data elements. 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 working with arrays. We can also use the NumPy package.
An array defined by the array module is ?
import array
arr = array.array('i', [1, 2, 3, 4])
print(arr)
array('i', [1, 2, 3, 4])
A NumPy array defined by the NumPy module is ?
import numpy as np arr = np.array([1, 2, 3, 4]) print(arr)
[1 2 3 4]
Also, we can use Python lists to represent arrays by storing homogeneous items ?
arr = [1, 2, 3, 4] print(arr)
[1, 2, 3, 4]
Input Output Scenarios
Assume we have an array with 5 elements. The output will display the last element.
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 array elements, we can use Python positive or negative index values ?
Positive index ? Python sequences are zero-indexed, which means indexing starts from 0 to n-1. The last element is available at total_items - 1 index.
Negative indexing ? Python negative indexing starts from -n to -1. The last element is available at -1 index.
Method 1: Using Lists with Negative Indexing
The simplest way to get the last element is using negative indexing with [-1] ?
# creating array
numbers = [11, 12, 13, 14, 15, 16, 17, 18, 19]
print("The original array is:", numbers)
# get last element using negative indexing
result = numbers[-1]
print("The last element is:", result)
The original array is: [11, 12, 13, 14, 15, 16, 17, 18, 19] The last element is: 19
Method 2: Using len() Function
The len() function returns the length of an object. We can use it to access the last element with len(list) - 1 ?
# creating array
numbers = [11, 12, 13, 14, 15, 16, 17, 18, 19]
print("The original array is:", numbers)
# get last element using len()
result = numbers[len(numbers) - 1]
print("The last element is:", result)
The original array is: [11, 12, 13, 14, 15, 16, 17, 18, 19] The last element is: 19
Method 3: Using NumPy Arrays
NumPy arrays also support negative indexing to access the last element ?
import numpy as np
# creating NumPy array
numpy_array = np.array([5, 10, 15, 20, 25])
print("The original array is:", numpy_array)
# get the last element
result = numpy_array[-1]
print("The last element is:", result)
The original array is: [ 5 10 15 20 25] The last element is: 25
Method 4: Using Array Module
The built-in array module also supports negative indexing ?
import array
# creating integer array
int_array = array.array('i', [1, 3, 4, 8])
print("The original array is:", int_array)
# get the last element
result = int_array[-1]
print("The last element is:", result)
The original array is: array('i', [1, 3, 4, 8])
The last element is: 8
Comparison
| Method | Syntax | Best For |
|---|---|---|
| Negative Indexing | arr[-1] |
Most readable and Pythonic |
| len() Function | arr[len(arr)-1] |
When explicit indexing is needed |
Conclusion
The most Pythonic way to get the last element from an array is using negative indexing with [-1]. This method works with lists, NumPy arrays, and array module objects, making it the preferred approach for accessing the last item.
