Open In App

Remove elements at Indices in List - Python

Last Updated : 24 Jun, 2025
Summarize
Comments
Improve
Suggest changes
Share
Like Article
Like
Report

In Python, lists store items in a specific order and each item has an index. Removing elements by index means creating a new list that excludes items at certain positions. This helps in keeping only the needed elements while discarding others based on their index.

For example:

Input : li= [5, 6, 3, 7, 8, 1, 2, 10]
idx = [2, 4, 5] 
Output : [5, 6, 7, 2, 10] 

Explanation: Elements at indices 2, 4, and 5 (i.e 3, 8 and 1)are removed from the list to create a new list without them.

Let’s explore different ways to remove items from specific indices.

Using del keyword

del keyword removes items from a list by specifying an index or a range (slice). It directly changes the original list.

Python
li = [10, 20, 30, 40, 50]
del li[1:4]
print(li)

Output
[10, 50]

Using enumerate() + list comprehension

Use enumerate() with list comprehension to loop through a list and keep only the items whose indices are not in the remove list. This creates a new list without the unwanted items in a short and clean way.

Python
li = [5, 6, 3, 7, 8, 1, 2, 10]
idx = [2, 4, 5]
res = [val for i, val in enumerate(li) if i not in idx]
print(res)

Output
[5, 6, 7, 2, 10]

Explanation: This code checks each item’s index using enumerate(). If the index is not in the remove list, the item is added to the new list using list comprehension.

Using enumerate() + loop

Use enumerate() in a loop to get each item and its index, then skip the items at the indices you want to remove. This creates a new list with only the needed items.

Python
li = [5, 6, 3, 7, 8, 1, 2, 10]
idx = [2, 4, 5]
res = []

for i, val in enumerate(li):
    if i not in idx:
        res.append(val)

print(res)

Output
[5, 6, 7, 2, 10]

Explanation: This code goes through each item in the list along with its index. If the index is not in the list of indices to remove, the item is added to a new list. This creates a new list without the unwanted elements.

Using pop()

pop() method removes an item from a list at a given index and returns its value. It updates the original list and gives you the removed element.

Python
a = [5, 6, 3, 7, 8, 1, 2, 10]
a.pop(1)
print(a)

Output
[5, 3, 7, 8, 1, 2, 10]

Related Articles:


Practice Tags :

Similar Reads