# Python Program to remove the last given number of items from the array

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

## Arrays in Python

In Python doesn’t have a native data structure to represent arrays. However, we can use Lists to represent arrays.

[1, 2, 3, 4, 5]


Also we can use array or NumPy modules to work with arrays in python.

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])


Python indexing is starts from 0. And the above all arrays are indexed from starting 0 to (n-1).

## Negative indexing

Python also supports negative indexing which is nothing but counting from the end of the array.

[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. In most of the examples below we will use the negative indices to remove the last given number of items.

## Input Output Scenarios

Assume we have an input array with 9 integer values. And in output the last few items will be removed based on the specified number.

Input array:
[1, 2, 3, 4, 5, 6, 7, 8, 9]
Output:
[1, 2, 3, 4, 5, 6]


The last 3 items 7, 8, 9 are removed from the input array.

## Using List

By using python list.pop() method we can remove the last element of an array. Following is the syntax to do so –

list.pop(index)


The pop() method removes an item from a given index and returns the removed item, by default it removes the last element.

### Example

In this example we will iterate the for loop up to the given number and then remove the last elements from the array.

# creating array
lst = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print ("The original array is: ", lst)
print()

numOfItems = 4
# remove last elements
for i in range(numOfItems):
lst.pop()
print ("The result is: ", lst)


### Output

The original array is:  [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
The result is:  [1, 2, 3, 4, 5, 6]


The last 4 elements 7, 8, 9, 10 are removed from the array.

### Example

Let’s take an example and apply list slicing to remove the last element from an array.

# creating array
lst = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print ("The original array is: ", lst)
print()

numOfItems = 4
# remove last elements
result = lst[:-numOfItems]
print ("The result is: ", result)


### Output

The original array is:  [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
The result is:  [1, 2, 3, 4, 5, 6]


By using list negative indexing and slicing technique we have removed the last 4 items from the given array. Here the original array list does not affect which remains the same.

## Using NumPy array

By using the numpy module and slicing technique we can easily remove the number of items from an array. Following is the syntax –

Numpy_array_obj[start:stop:step]


### Example

Let’s take a NumPy array and remove the last 2 elements.

import numpy

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

numOfItems = 2

# remove last elements
result = numpy_array[:-numOfItems]
print ("The result is: ", result)


### Output

The original array is:  [1 7 9 4 9]
The result is:  [6 6 3]


We have successfully removed the last 2 elements from the numpy array using the negative indexing.

## Using the array module

The python array module also supports the array indexing, and slicing techniques to access the elements.

### Example

In this example, we will create an array using the array module.

import array
# creating array
arr = array.array('i', [2, 4, 6, 8, 10])
print ("The original array is: ", arr)
print()
numOfItems = 2
# remove last elements
result = arr[:-numOfItems]
print ("The result is: ", result)


### Output

The original array is:  array('i', [2, 4, 6, 8, 10])
The result is:  array('i', [2, 4, 6])


The last 2 elements are removed from the array.

Updated on: 29-May-2023

40 Views 