Remove multiple indexes in Python List

Python List

In this post, We are going to explore Remove multiple indexes in Python List. As a Data Analyst, While doing the data manipulation, we have the need to delete or remove multiple indexes at the same time. But in Python built-in functionals make our work a little bit easy.

1.Custome function to remove multiple indexes


In this code example, we have defined a custom function remove_mul_ele() and we are looping over the indexes that we have to delete from the original list. We are removing elements at the given index using the list pop() method.

mylist = ['C#','lang','Go','Data','C#','16','17','35','68']

# remove the element at indexs 3,5,6

item_remove_indexes = [3, 5, 6]
 
#function to remove at mutiple indexs

def remove_mul_ele(input_list,list_of_indexes):
    indexes = sorted(list_of_indexes, reverse=True)
    for id in indexes :
        if id < len(input_list):
            input_list.pop(id)
    print('list after remove multiple indexes =\n',input_list)          
#calling the remove_multiple_element
remove_mul_ele(mylist, item_remove_indexes)

Output

list after remove multiple indexes  =
 ['C#', 'lang', 'Go', 'C#', '35', '68']

2. enumerate to Remove multiple indexes


In this code example, we are using enumerate() method. The Python enumerate() function is used to add a counter to an iterable (list, tuple, dictionary). We are passing list(mylist) iterable to enumerate() method and using if condition for filtering of the indexes. This will delete the elements at the indexes which are at index 3,5,6. The resulting list will contain the rest of the elements after deletion.

mylist = ['C#','lang','Go','Data','C#','16','17','35','68']

#remove the element at indexes 3,5,6

inexes_to_remove = [3, 5, 6]

resultlist = [i for j, i in enumerate(mylist) if j not in inexes_to_remove]

print('list after removed multiples indexes:\n',resultlist)

Output

list after removed multiples indexes:
 ['C#', 'lang', 'Go', 'C#', '35', '68']

3. del keyword to delete range of indexes from a list


In this code example, we are passing the range of indexes to the del method. Here in this code, we are providing a range of indexes. If you see the input which we are passing, it is between [1:3] means it will delete elements at 1st index and 2nd index. Index 3 is not included in this range.

mylist = ['C#','lang','Go','Data','C#','16','17','35','68']

#removing a range of indexes

del mylist[1:3]
print('after removing multiple indexes: \n',mylist)

Output

after removing multiple indexes: 
 ['C#', 'Data', 'C#', '16', '17', '35', '68']

4. Numpy to Remove multiple indexes from a list


In this code example, First of all, we are converting the list(mylist) into a NumPy array, later we are using the NumPy delete() method and passing indexes that we want to delete from the list.

First, need to import the numpy library using import numpy as np

This example is making use of the Numpy library function. If you want to learn more about the Numpy library of Python then please read it here.

import numpy as np
mylist = ['C#','lang','Go','Data','C#','16','17','35','68']

#removing a range of element

item_remove_indexes = [3, 5, 6]
arr = np.array(mylist)


reslist = list(np.delete(arr, item_remove_indexes))
print('after removing multiple element: \n',reslist)

Output

after removing multiple element: 
 ['C#', 'lang', 'Go', 'C#', '35', '68']

Summary:

In this post, we have explored explore Remove multiple indexes in Python List using customer function, del keyword, Numpy delete() method, and enumerate.

Reference : stackoverflow