How to delete multiple items from a list in Python

Deleting multiple items from a list returns the list without the items. For example, deleting the items at indecies 0 and 3 in [“apple”, “banana”, “pear”, “peach”] returns [“banana”, “pear”].

Solution for How to delete multiple items from a list in Python : You can use del and sorted() to delete multiple items from a list Call sorted(index_list, reverse) with index_list as the list of indecies to delete from the original list and reverse as True to ensure that the list of indecies to delete is in order from greatest to least.

It is crucial that the list of indecies is in reverse order so that, when a value is deleted, the values to which the rest of the indecies refer will remain the same.

Use a for-loop to call del list[index] with list as the original list for each index in the list of indecies to delete to delete that index in the original list.


how-to-delete-multiple-items-from-a-list-in-python