A python list is a collection of items of same or different types.
This article will show different methods to remove items from a list while iterating in python.
The item will be deleted based on some condition.
1. Using for loop and range function
Iterate over the list backwards and remove the element if it matches certain condition.
Below code declares a list of string elements. We need to remove elements that start with letter ‘d’.
It iterates over the list using for
loop and range
function backwards.
In every iteration, it checks if the current element starts with ‘d’ and removes it.
Element is removed using del
function of list. It takes an element as argument and removes it from the list.
Also, to check if list element begins with letter ‘d’, startswith
function is used.
# declare list element_list = ['abc', 'def', 'dfv', 'acb', 'xyz'] print("List before deletion:", element_list) # iterate over the list for i in range(len(element_list) - 1, -1, -1): # check if element begins with 'd' if element_list[i].startswith('d'): # remove it del(element_list[i]) # print list after deletion print("List after deletion:", element_list)
range
function takes three arguments.
First is the start index which is [length of list – 1], that is, the index of last list element(since index of list elements starts from 0 till length – 1).
Second argument is the index at which to stop iteration.
Since we need to compare all elements, this value must be -1. If this value is 0, then first element will not be checked.
Third argument is the step size.
Since we need to decrease index by 1 in every iteration, this should be -1.
Output is
List before deletion: [‘abc’, ‘def’, ‘dfv’, ‘acb’, ‘xyz’]
List after deletion: [‘abc’, ‘acb’, ‘xyz’]
Method 2: Using while loop
Above method iterates over the list backwards.
If you need to iterate it in forward direction, the use a while
loop as shown below.
# declare list element_list = ['abc', 'def', 'dfv', 'acb', 'xyz'] print("List before deletion:", element_list) # list index variable index = 0 # iterate over the list while index < len(element_list): # check if element begins with 'd' if element_list[index].startswith('d'): # remove it del(element_list[index]) else: index += 1 # print list after deletion print("List after deletion:", element_list)
It iterates over the list using a while
loop starting from first till last element.
In every iteration, it checks the condition and removes the element if the condition matches otherwise loop index is incremented for the next element.
Note that if the element is removed, loop index is not incremented. This is because when the element is removed, index of remaining elements are reduced by 1.
In this case, if we increment the loop index, then some elements may be skipped from comparison.
Output of execution is
List before deletion: [‘abc’, ‘def’, ‘dfv’, ‘acb’, ‘xyz’]List after deletion: [‘abc’, ‘acb’, ‘xyz’]
Other methods
For deleting an element from the list, instead of using del function, following two functions can also be used.
1. remove()
This method of list class can also be used to remove an item from the list.
This method takes the element to be removed from the list and removes it from the list.
Thus, in place of del function, this method can be used as element_list.remove(element_list[index])
2. pop()
This method of list class takes the index of a list item and removes the item at that index.
It can be used as element_list.pop(index)
Hope the article was useful.