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.
Similar Reads
-
Find middle element of a linked list in single pass
Linked lists are a fundamental data structure used in computer science for organizing and managing data. A linked list is… -
Python Program to Find Last 3rd element in Singly Linked List
In this tutorial, we are going to learn the writing python program to Find 3rd element of Linked List from… -
Most important JavaScript Interview Questions To Prepare
In this Page we have collected and explained Most important Javascript Interview Questions and Answers for begineers, freshers as well… -
Sum of digits of Given Number in Java
In this tutorial we will learn writing Java program to calculate the sum of its digit. We will also see… -
Hibernate Interview Questions for 2+ years of experience
Certainly! Here's a list of commonly asked interview questions on Hibernate for candidates with 2+ years of experience: Basic Hibernate… -
68 Most Important Microservices Interview Questions
Certainly, here's an extended list of 50 commonly asked interview questions on microservices for candidates with 2+ years of experience:… -
60 Most Important Git Interview Questions
Certainly! Here is a list of commonly asked interview questions on Git for candidates with fresher or having of experience… -
50+ Most important Java Interview Questions for 5+ Years Exp
1. Explain the SOLID principles in Java. Provide examples of how you have applied these principles in your projects. SOLID… -
60+ Spring Boot interview questions for 4+ years Exp.
1. What is Spring Boot and how does it differ from the Spring framework? Spring Boot is a framework designed… -
60+ Mostly Asked Spring Boot Interview Questions for 3+ Yrs
Here is a list of 60+ Spring Boot interview questions for candidates with 3+ years of experience: 1. What is…