- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python Program to Remove the last element from an Array
There are three different ways in order to delete or remove the element. Let us discuss some of those methods and keywords that are used to remove the last element from an array one after another in a row.
Using Delete() Method of the Numpy Module
This module can be used in order to delete an element of an array when an index is clearly specified. This operation can be done by the delete() method that belongs to the module numpy. But, in order to use that delete method, the array should be created in the form of Numpy Array.
Working of Delete() Method
The delete() method is used to remove the element of the array or the list by mentioning the index of the element that should be removed. The syntax of the usage of the method delete() is described below.
Syntax
variable = n.delete(arr, last_index)
Example
In this example, we are going to discuss about the process of removing the last element of an array by using delete() method of Numpy module.
import numpy as n arr = [" Hello ", " Programming ", " Python ", " World ", " Delete ", " Element "] variable = n.array(arr) max_size = len(variable) last_index = max_size - 1 print(" The elements of the array before deletion: ") print(variable) variable = n.delete(arr, last_index) print(" The elements of the array after deletion: ") print(variable)
Output
The output of the above program is as follows −
The elements of the array before deletion: [' Hello ', ' Programming ', ' Python ', ' World ', ' Delete ', ' Element '] The elements of the array after deletion: [' Hello ', ' Programming ', ' Python ', ' World ', ' Delete ']
Using “ del ” Keyword
The keyword del is used to delete the objects in Python programming language. Not only objects, the keyword del can also be used to delete the elements of the lists, arrays, etc. Let us use this keyword and delete the last element of the array.
Syntax
del arr[last_index]
Example
In this example, we are going to discuss about the process of removing the last element of an array by using del keyword.
arr = [" Hello ", " Programming ", " Python ", " World ", " Delete ", " Element "] max_size = len(arr) last_index = max_size – 1 print(" The elements of the array before deletion: ") print(arr) print(" The elements of the array after deletion: ") del arr[last_index] print(arr)
Output
The output of the above program is as follows −
The elements of the array before deletion: [' Hello ', ' Programming ', ' Python ', ' World ', ' Delete ', ' Element '] The elements of the array after deletion: [' Hello ', ' Programming ', ' Python ', ' World ', ' Delete ']
By Using pop() Method
The method pop() is used to delete the elements of the arrays, lists, etc in Python programming language. This mechanism works with the usage of the index of the element that must be removed or deleted from the array. The element simply pops up from the array and will be removed. Let us use this method and delete the last element of the array.
Syntax
arr.pop(last_index)
Example
In this example, we are going to discuss about the process of removing the last element of an array by using pop() method
arr = [" Hello ", " Programming ", " Python ", " World ", " Delete ", " Element "] max_size = len(arr) last_index = max_size -1 print(" The elements of the array before deletion: ") print(arr) print(" The elements of the array after deletion: ") arr.pop(last_index) print(arr)
Output
The output of the above program is as follows −
The elements of the array before deletion: [' Hello ', ' Programming ', ' Python ', ' World ', ' Delete ', ' Element '] The elements of the array after deletion: [' Hello ', ' Programming ', ' Python ', ' World ', ' Delete ']
Conclusion
We can observe that the outputs of all three programs that are discussed above are completely equal which proves that the last element is successfully removed from the array by using all three ways. In this way, the deletion of an element of any index in an array can performed very easily by using simple techniques.