Python Program to Delete Element at Given Index in Array

In this tutorial you will learn to write Python Program to Delete an element at Given index in Array. There are various approach to write this program. In this tutorial we will see some approaches like using slicing, using pop() and using del in Python.

For Examples:

Let’s consider the following array:

arr = [1, 2, 3, 4, 5]

Suppose we want to delete element from index 2. In above array at index 2, element is 3. So after deletion, the array will be

arr = [1, 2, 4, 5]

Program 1: Using the pop() method

With the help of pop() method we can delete an element from an array at a given index. To delete at given index, we have to pass index number to the pop() method. Below is the program in python.

arr = [1, 2, 3, 4, 5]
arr.pop(2)
print("Array after removing from index : ", arr) 

Output

Array after removing from index :  [1, 2, 4, 5]

Program 2: Using the del keyword

The simplest way to delete an element from an array at a given index is to using the del keyword. Below is the program using del keyword to delete element from array at given location.

arr = [1, 2, 3, 4, 5]
del arr[2]
print("Array after removing from index : ", arr) 

Output :

Array after removing from index :  [1, 2, 4, 5]

Program 3: Using Slicing Keyword

With the help of Slicing We can also delete an element from an array at a given index. We will create two slices of the array. One slice will be before the index and one slice will be after the index, and then concatenate them. Below is the program.

arr = [1, 2, 3, 4, 5]
arr = arr[:2] + arr[3:]
print("Array after removing from index : ", arr) 

Output:

Array after removing from index :  [1, 2, 4, 5]

Conclusion:

In this above post we have learned various ways to delete an element from an array at a given index in Python Programing language. We have seen the various approaches like using del keyword, the pop() method, and slicing technique. Hope this tutorial helped you in to understand python program.