This article will discuss different ways to remove single or multiple elements from a list by the index positions.


Table of Contents

Python: Remove an element from a list by index using the pop() function

In Python, the list class provides a function pop(index) to remove an item from the list at the given index. But if the list is empty or the given index is out of range, then the pop() function can raise IndexError. Therefore we should carefully use this function to delete an item from a list by index position.

We have created a function for deleting an element from a list by index. It internally uses the pop() function but first checks if the given index is valid or not. Let’s understand by an example,

def delete_element(list_object, pos):
    """Delete element from list at given index
     position (pos) """
    if pos < len(list_object):
        list_object.pop(pos)



list_of_num = [51, 52, 53, 54, 55, 56, 57, 58, 59]

delete_element(list_of_num, 5)

print(list_of_num)

Output:

[51, 52, 53, 54, 55, 57, 58, 59]

The function delete_element() accepts two arguments,

  • A list, from where element needs to be deleted.
  • An index, that represents the position at which element needs to be deleted from the given list.

To avoid the IndexError by the pop() function, delete_element() first checks if the given index position is valid or not. If yes, then it deletes the element at the given index position by calling the pop() function.

Let’s check out another example in which we are trying to delete an element by an index that is out of range,

list_of_num = [51, 52, 53, 54, 55, 56, 57, 58, 59]

delete_element(list_of_num, 15)

print(list_of_num)

Output:

[51, 52, 53, 54, 55, 56, 57, 58, 59]

It did not affect the list because, in function delete_element(), it checked if the given index is valid or not. If we use pop() function directly with this index, it will raise index error. For example,

list_of_num = [51, 52, 53, 54, 55, 56, 57, 58, 59]

list_of_num.pop(15)

Error:

    list_of_num.pop(15)
IndexError: pop index out of range

Python: Remove an element from the list by index using del keyword

We can delete an item from the list by index position using “del list_object [index]”. But if the list is empty or the given index is out of range, then del keyword can raise IndexError. Let’s understand by an example,

list_of_num = [51, 52, 53, 54, 55, 56, 57, 58, 59]

# Delete the element at index 5
del list_of_num[5]

print(list_of_num)

Output:

[51, 52, 53, 54, 55, 57, 58, 59]

It deleted the element at index position 5. But what if we try to delete an element with an index that is out of range. For example,

list_of_num = [51, 52, 53, 54, 55, 56, 57, 58, 59]

# Delete the element at index 15
del list_of_num[15]

Error:

    del list_of_num[15]
IndexError: list assignment index out of range

It raised an IndexError because the given index was out of bounds. The index position was greater than the length of the list. So, to avoid this index error, we should always check if the given index is valid or not. For example,

list_of_num = [51, 52, 53, 54, 55, 56, 57, 58, 59]

idx = 15
if idx < len(list_of_num):
    del list_of_num[idx]

print(list_of_num)

Output:

[51, 52, 53, 54, 55, 56, 57, 58, 59]

Python: Remove an element from list by index using slicing

In all the previous solutions, we modified the list in place. But if we want a new list by removing the element at a given index from the original list, we can use slicing. For example, to delete an element at index N, slice the list into three pieces,

  • Elements from index position 0 to N-1
  • Element at index position N
  • Elements at index position N+1 till the end of the list.

Now join back slices 1 and 3 to create a new list. For example,

list_of_num = [51, 52, 53, 54, 55, 56, 57, 58, 59]

index = 5
# Remove element at index 5 from the list
list_of_num = list_of_num[:index] + list_of_num[index+1 : ]

print(list_of_num)

Output:

[51, 52, 53, 54, 55, 57, 58, 59]

We created a new list from the original list by deleting the element at index position 4. We then assigned this new list to the variable referring to the original list. It gave an effect that we have deleted an element from a list by index position.

Python: Remove multiple elements from a list by indices

Suppose we have a list of 10 elements, and we want to delete elements at index 2, 4, and 6. We can not just iterate over the given index positions and call the pop(index). The pop() function will delete the element at a given index, and due to that index position of all the elements after the deleted elements will change (decrease by 1).

The best solution is to sort the index positions in decreasing order and start calling the pop() function on index positions highest to lowest. We have created a function to make this process easy. It can delete multiple elements from a list based on the given index positions. It accepts two arguments,

  • A list from which we need to delete the elements.
  • A list of index positions at which items need to be deleted.

Let’s use this function to remove items of a list at indices 2, 4, and 6,

def delete_multiple_element(list_object, indices):
    indices = sorted(indices, reverse=True)
    for idx in indices:
        if idx < len(list_object):
            list_object.pop(idx)


list_of_num = [51, 52, 53, 54, 55, 56, 57, 58, 59]

list_of_indices = [4, 2, 6]

# Remove elements from list_of_num at index 4,2 and 6
delete_multiple_element(list_of_num, list_of_indices)

print(list_of_num)

Output:

[51, 52, 54, 56, 58, 59]

It first sorted the index positions to 6, 4 & 2 and then iterated over the index positions. For each index position, it validated if the index is valid or not. If valid, then deleted the element at that index by calling the pop(index) function.

Why sort the index positions in decreasing order before calling the pop() function?

On calling the pop(6), the element at index position 6 got deleted. But the index positions of all the elements after the deleted element got decremented by 1. But for elements before the deleted element, there was no change in index positions. As our indices are sorted in decreasing order, so we can safely delete the remaining items.

Python: Remove items from a list by index range

Suppose we have a list, and we want to delete the elements by an index range, i.e., from index position 2 to 6. For that, we can use,

del list_obj[start:end]

We can select the elements from a list based on index range (start to end-1), and then we can pass those selected elements to the del keyword. It will delete the items in the list from index position start to end-1. For example,

list_of_num = [51, 52, 53, 54, 55, 56, 57, 58, 59]

# remove elements from index 2 to 5
del list_of_num[2:5]

print(list_of_num)

Output:

[51, 52, 56, 57, 58, 59]

It deleted the elements from index positions 2 to 4.

Summary

This article explained different ways to delete an element from a list by index position. Then we also explored how to delete multiple elements from a list by index positions or indices.

Join a list of 2000+ Programmers for latest Tips & Tutorials