In this tutorial we will learn writing Python Program to Delete element at End of Array. Arrays are mostly used data structures in programming language. We can store multiple data/elements in it that have same data type.
Suppose we have an array arr = [1, 2, 3, 4, 5]. After deletion the last element from the array, Array will become [1, 2, 3, 4].
Program 1: Using pop() function
Here to delete the element at the end of an array we are using built-in function pop()
of the python. Basically pop()
method is used to removes the element from the array at given index. If no index is specified then it will removes the last element of the array.
arr = [1, 2, 3, 4, 5]
arr.pop()
print("Array after delete at end:", arr)
Output:
Array after delete at end: [1, 2, 3, 4]
Program 2: Using slicing
arr = [1, 2, 3, 4, 5]
#removing the last element using slicing
arr = arr[:-1]
#printing the updated array
print("Array after delete at end:", arr)
Output:
Array after delete at end: [1, 2, 3, 4]
Program 3: Using del keyword
arr = [1, 2, 3, 4, 5]
#removing the last element using del keyword
del arr[-1]
#printing the updated array
print("Array after delete at end:", arr)
Output:
Array after delete at end: [1, 2, 3, 4]
Conclusion
In this post we have learned various ways to write a program in python to delete the element at the end of an array. We can use the pop()
method, slicing technique, or the del
keyword to remove the last element of an array. Hope this tutorial helped you in learning python code.