Given an array, the task is to write a Python program to remove the last element in Python.
Example:
Input: [“neveropen”, “for”, “neveropen”]
Output: [“neveropen”, “for”]
Input: [1, 2, 3, 4, 5]
Output: [1, 2, 3, 4]
Explanation: Here simply we have to remove the last element present in the
array and return the remaining array.
Note: Arrays are not supported in Python directly but they can be imported from the array module and used for the basic array operations.
Remove the last item from array Using Slicing Technique
Python3
# importing array from array module import array as a # initializes array with signed integers arr = a.array( 'i' , [ 1 , 2 , 3 , 4 , 5 ]) # printing original array print ( "The original array is : " , end = "") for i in range ( 0 , 5 ): print (arr[i], end = " " ) print () # using negative index to find last element print ( "The last element is : " , end = "") print (arr[ - 1 ]) # using slicing technique arr = arr[: - 1 ] print ( "The array after removing last element is : " , end = "") for i in range ( 0 , 4 ): print (arr[i], end = " " ) print () |
Output:
The original array is : 1 2 3 4 5
The last element is : 5
The array after removing last element is : 1 2 3 4
Explanation:
Here we simply find the last element using the negative indexing method and then update the original array with the updated one using the concept of slicing and removing the last element present in the array.
Remove the last item from array Using pop() method
Python3
# importing array from array module import array as a # initializes array with signed integers arr = a.array( 'i' , [ 1 , 2 , 3 , 4 , 5 ]) # printing original array print ( "The original array is : " , end = "") for i in range ( 0 , 5 ): print (arr[i], end = " " ) print () # using pop() to remove element at # the last position print ( "The popped element is : " , end = "") print (arr.pop()) print ( "The array after removing last element is : " , end = "") for i in range ( 0 , 4 ): print (arr[i], end = " " ) print () |
Output:
The original array is : 1 2 3 4 5
The popped element is : 5
The array after removing last element is : 1 2 3 4
Explanation:
Here we have directly used the pop() method that removes the last element in the array and then prints the resultant array.
Remove the last item from array Using del() method
Python3
import array as a # initializes array with signed # integers arr = a.array( 'i' , [ 1 , 2 , 3 , 4 , 5 ]) # printing original array print ( "The original array is : " , end = "") for i in arr: print (i, end = " " ) print () # using pop() to remove element at # the last position print ( "The popped element is : " , end = "") print (arr[ - 1 ]) del arr[ - 1 ] print ( "The array after removing last element is : " , end = "") for i in arr: print (i, end = " " ) print () |
Output:
The original array is : 1 2 3 4 5
The popped element is : 5
The array after removing last element is : 1 2 3 4
Note:
There may be some more methods like reversing the array and removing the first element but again the concept is same.
Remove the last item from array Using remove()
Python3
import array as a # initializes array with signed # integers arr = a.array( 'i' , [ 1 , 2 , 3 , 4 , 5 ]) # printing original array print ( "The original array is : " , end = "") for i in arr: print (i, end = " " ) print () # using remove() to remove element at # the last position print ( "The removed element is : " , end = "") print (arr[ - 1 ]) arr.remove(arr[ - 1 ]) print ( "The array after removing last element is : " , end = "") for i in arr: print (i, end = " " ) print () |
Output:
The original array is : 1 2 3 4 5
The removed element is : 5
The array after removing last element is : 1 2 3 4
Note: Here we use the remove method on the array by accessing the last element of an array using array indexing.