In this post, We are going to explore how to Remove elements by multiple indices Python List. As Data analysts, While doing the data manipulation, we have the need to delete or remove multiple indexes at the same time. But in Python built-in functions make our work a little bit easy.
1. Custom function to remove multiple indices in Python List
In this code example, we will discuss How to Remove elements by multiple indices Python List by defining a custom function remove_mul_ele() and we are iterating through the indexes that we have to delete from the original list.
Secondly, use if condition id < len(input_list) for each iteration that removes the given index from the original list by index using the list pop() method..Once the condition the loop terminates and list after removing the given indexes will print
#python 3 code -How to Remove elements by multiple indices Python List 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)
list after remove multiple indexes = ['C#', 'lang', 'Go', 'C#', '35', '68']
- Remove multiple elements from the python list
- Python List create add update delete
- 6 Methods to Remove duplicates from Python list
- 5 Ways to Remove duplicates list of Python dictionaries
2. Enumerate to Remove multiple indices
In this code example, We will learn how to Remove elements by multiple indices Python List by 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 given indexes. This will delete the elements at given indexes which are 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)
list after removed multiples indexes: ['C#', 'lang', 'Go', 'C#', '35', '68']
3. del keyword to delete a range of indices 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)
after removing multiple indexes: ['C#', 'Data', 'C#', '16', '17', '35', '68']
4. Numpy to Remove multiple indexes in Python List
In this python 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.
To use the NumPy library first need to import the NumPy library using import NumPy as np and make sure the NumPy library is installed on the local system. 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)
after removing multiple element: ['C#', 'lang', 'Go', 'C#', '35', '68']
In this post, we have learned how to Remove multiple indices in Python List using customer function, del keyword, Numpy delete() method, and enumerate with code example.
Reference : stackoverflow