
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Get Last N Items from an Array in Python
An array is a data structure consisting of many elements with the same data type, and each element is identified by an index.
[2, 4, 0, 5, 8]
Arrays in Python
Python does not have its own data structure to represent an array. However, we can use the list data structure as an alternative to the arrays. Here we will use list an array:
[10, 4, 11, 76, 99]
python provides some modules to work with arrays more appropriately which are Numpy, and array modules.
In this article, we will see different ways to access the last given number of elements from an array.
Input Output Scenarios
Assume we have an input array with 9 integer values. And in the output, last few items are accessed based on the specified number.
Input array: [1, 2, 3, 4, 5, 6, 7, 8, 9] Output: [7,8,9]
The last 3 items 7, 8, 9 are accessed from the input array.
Input array: [10, 21, 54, 29, 2, 8, 1] Output: [29, 2, 8, 1]
Last 4 items are retrieved from the input array.
In the below examples, we will mainly use python negative indexing and slicing features to retrieve the last few elements.
Negative indexing in python
Python supports negative indexing also which is nothing but counting elements from the end of the array with the negative sign and it starts from 1, not from 0.
[1, 2, 3, 4, 5] -5 -4 -3 -2 -1
The first element is identified by the index value -n and last element is -1.
Slicing in python
With the slicing feature in python accessing the group of elements from a sequence is possible by the shortest syntax.
Syntax
sequence_object[start : end : step]
Start: The starting index where the slicing of the iterable starts. by default it is 0.
End: The ending index where the slicing of the list stops. The default value is the length of the iterable object. And this value is excluded.
Using List
By using the list-slicing feature we can access the last given number of elements from an array.
Example
Let's take an example and apply list slicing to access the last few elements from an array.
# creating array lst = [1, 2, 0, 4, 2, 3, 8] print ("The original array is: ", lst) print() numOfItems = 4 # Get last number of elements result = lst[-numOfItems:] print ("The last {} number of elements are: {}".format(numOfItems, result))
Output
The original array is: [1, 2, 0, 4, 2, 3, 8] The last 4 number of elements are: [4, 2, 3, 8]
The last 4 elements are accessed from the given array using the negative index.
Using NumPy array
Let's use a NumPy array to access the last given number of elements.
Example
In this example, we will access the numpy array elements with the help of negative index values.
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[-numOfItems:] print ("The last {} number of elements are: {}".format(numOfItems, result))
Output
The original array is: [4 6 9 7 5] The last 2 number of elements are: [7 5]
We have successfully accessed the last 2 elements from the NumPy array. Element 7 is indexed with the -2 and 5 is indexed with the -1.
Using the array module
By using the array() method we will create an array of specific data type.
Example
In this example, we will create an array using the array module.
import array # creating array arr = array.array('i', [6, 5, 8, 7]) print ("The original array is: ", arr) print() numOfItems = 2 # remove last elements result = arr[-numOfItems:] print ("The last {} number of elements are: {}".format(numOfItems, result))
Output
The original array is: array('i', [6, 5, 8, 7]) The last 2 number of elements are: array('i', [8, 7])
From the above example the requested number of items are accessed successfully. The python slicing does not generate any error if the requested number of elements exceeds the total number of elements in a sequence.