Python – Remove elements at Indices in List
Given List, remove all the elements present in the indices list in Python.
Input : test_list = [5, 6, 3, 7, 8, 1, 2, 10], idx_list = [2, 4, 5] Output : [5, 6, 7, 2, 10] Explanation : 3, 6, and 1 has been removed. Input : test_list = [5, 6, 3, 7, 8, 1, 2, 10], idx_list = [2] Output : [5, 6, 7, 8, 1, 2, 10] Explanation : 3 has been removed.
In this article, we will cover how to Remove items at a specific index from Python List, and cover different methods that are listed below:
- Remove an item by index and get its value using pop()
- Remove items by index or slice using del.
- Remove items at a specific index using enumerate() + loop
- Remove items at a specific index using enumerate() + list comprehension
Method 1: Remove an item by index and get its value using pop()
In this example, we will use the pop method to remove the element from the list, here in the pop we will pass the index value to remove the element at that position.
Python3
# initializing listtest_list = [5, 6, 3, 7, 8, 1, 2, 10]test_list.pop(1)print(test_list) |
Output:
[5, 3, 7, 8, 1, 2, 10]
Time complexity: O(n), where n is the length of the list.
Auxiliary space: O(1), since the operation does not require any additional space besides the list itself.
Method 2: Remove items by index or slice using del
In this example, we will use the del keyword to delete the specific elements present in the list. Here we will remove multiple items from the list by index. Please note that we need to sort the indices in reversed order to ensure that the shift of indices induced by the deletion of elements at lower indices won’t invalidate the index specifications of elements at larger indices.
Python3
test_list = [5, 6, 3, 7, 8, 1, 2, 10, 5]indices=[3,7]for i in sorted(indices, reverse=True): del test_list[i] print(test_list) |
Output:
[5, 6, 3, 8, 1, 2, 5]
Time complexity: O(nlogn), where n is the length of the test_list.
Auxiliary Space: O(1), constant extra space is required
Method 3: Remove items at a specific index using enumerate() + loop
In this, we iterate for all the elements, and if the index is present in the list, then that index element is omitted from the result list.
Python3
# Python3 code to demonstrate working of# Remove elements at Indices in List# Using loop# initializing listtest_list = [5, 6, 3, 7, 8, 1, 2, 10]# printing original listprint("The original list is : " + str(test_list))# initializing idx listidx_list = [2, 4, 5, 7]res = []for idx, ele in enumerate(test_list): # checking if element not present in index list if idx not in idx_list: res.append(ele)# printing resultsprint("Filtered List after removal : " + str(res)) |
Output:
The original list is : [5, 6, 3, 7, 8, 1, 2, 10] Filtered List after removal : [5, 6, 7, 2]
Time complexity: O(n), where n is the length of the input list test_list
Auxiliary space: O(n), where n is the length of the input list test_list.
Method 4: Remove items at a specific index using enumerate() + list comprehension
In this, we perform the task of iteration using list comprehension in a compact way, the rest of the methods are similar to the above.
Python3
# Python3 code to demonstrate working of# Remove elements at Indices in List# Using enumerate() + list comprehension# initializing listtest_list = [5, 6, 3, 7, 8, 1, 2, 10]# printing original listprint("The original list is : " + str(test_list))# initializing idx listidx_list = [2, 4, 5, 7]# one-liner to test for element in index listres = [ele for idx, ele in enumerate(test_list) if idx not in idx_list]# printing resultsprint("Filtered List after removal : " + str(res)) |
Output:
The original list is : [5, 6, 3, 7, 8, 1, 2, 10] Filtered List after removal : [5, 6, 7, 2]
Time Complexity: O(n), where n is the length of the original list test_list.
Auxiliary Space: O(m), where m is the length of the index list idx_list.


Please Login to comment...