Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to remove an element from a list by index in Python?
In this article, we will show you how to remove an element from a list by index using Python. Here we see 4 methods to accomplish this task ?
Using the del keyword to remove an element from the list
Using the pop() function to remove an element from the list
Using slicing to remove an element from the list
Using indices to remove multiple elements from the list
Assume we have taken a list containing some elements. We will remove a specific item from a list by giving the index value using the above-specified methods.
Using the del Keyword
The del keyword deletes the element at the specified index position. It modifies the original list in-place.
Syntax
del list_object[index]
If the list is empty or the specified index is out of range, the del keyword will throw an IndexError.
Example
The following program returns the list after deleting the specified index list element using the del keyword ?
# input list
inputList = ["Welcome", "to", "tutorialspoint", "python"]
# Enter the index at which the list item is to be deleted
givenIndex = 3
# deleting the list item at the given index using the del keyword
del inputList[givenIndex]
# printing the list after deleting a specified list item
print("List after deleting specified list item:", inputList)
The output of the above code is ?
List after deleting specified list item: ['Welcome', 'to', 'tutorialspoint']
Using the pop() Function
The pop() function removes and returns the element at the specified index. Unlike del, it returns the removed element.
Example
The following program returns the list after deleting the specified index list element using the pop() function ?
inputList = ["Welcome", "to", "tutorialspoint", "python"]
givenIndex = 1
# Passing index to the pop function to remove that element of the list at that index
removed_element = inputList.pop(givenIndex)
# printing the list after deleting an item at the entered index
print("List after deleting an item at the entered index:")
print(inputList)
print("Removed element:", removed_element)
The output of the above code is ?
List after deleting an item at the entered index: ['Welcome', 'tutorialspoint', 'python'] Removed element: to
Using Slicing
Slicing creates a new list by combining elements before and after the target index, effectively removing the element at that position.
To delete an element at index N, we combine:
Elements from index 0 to N-1
Elements from index N+1 to the end of the list
Example
The following program demonstrates removing an element using slicing ?
inputList = ["Welcome", "to", "tutorialspoint", "python"]
givenIndex = 3
# storing the list elements before the given index and after the given index
# using slicing which means it removes the item at the given index
inputList = inputList[:givenIndex] + inputList[givenIndex+1:]
# printing the list after deleting an item at the entered index
print("List after deleting an item at the entered index:\n", inputList)
The output of the above code is ?
List after deleting an item at the entered index: ['Welcome', 'to', 'tutorialspoint']
Removing Multiple Elements by Index
To remove multiple elements by their indices, we sort the indices in descending order and remove them one by one. This prevents index shifting issues.
Example
The following program removes multiple elements by their indices ?
inputList = ["Welcome", "to", "tutorialspoint", "python"]
# Entering the indices list at which the items are to be deleted
givenIndices = [1, 3]
# Reversing Indices List to avoid index shifting
indicesList = sorted(givenIndices, reverse=True)
# Traversing in the indices list
for indx in indicesList:
# checking whether the corresponding iterator index is less than the list length
if indx < len(inputList):
# removing element by index using pop() function
inputList.pop(indx)
# printing the list after deleting items at the given indices
print("List after deleting items at the given indices:\n", inputList)
The output of the above code is ?
List after deleting items at the given indices: ['Welcome', 'tutorialspoint']
Comparison
| Method | Returns Value? | Creates New List? | Best For |
|---|---|---|---|
del |
No | No (modifies original) | Simple deletion |
pop() |
Yes (removed element) | No (modifies original) | When you need the removed value |
| Slicing | No | Yes | Functional programming approach |
| Multiple removal | No | No (modifies original) | Removing multiple elements |
Conclusion
Use del for simple element removal, pop() when you need the removed value, and slicing for a functional approach. For multiple removals, sort indices in descending order to avoid shifting issues.
