Python Array – Remove Last Item

Remove last item in Array in Python

In Python, you can remove the last item in a given array using pop() method of the array instance.

In this tutorial, you will learn how to remove the last item in the given Python array, with examples.

To remove the last item in given array my_array in Python, call the pop() method on the array object, and pass no argument to the pop() method as shown in the following code snippet.

my_array.pop()

The pop() method removes the last item from the array and returns the removed item.

1. Remove last item in given integer array

In the following program, we take an integer array my_array with some initial values, and then use pop() method to remove the last item in this array.

Python Program

import array as arr

my_array = arr.array('i', [10, 15, 20, 25, 60, 20, 40])
x = my_array.pop()

print("Item removed :", x)
print("Result array :", my_array)
Run Code Copy

Output

Item removed : 40
Result array : array('i', [10, 15, 20, 25, 60, 20])

Explanation

Array :   10, 15, 20, 25, 60, 20, 40
                                  ↑
                          remove last item
Result :  10, 15, 20, 25, 60, 20

Summary

In this Python Array tutorial, we learned how to remove the last item in a Python Array, using pop() method, with examples.

Related Tutorials

Code copied to clipboard successfully 👍