Managing lists is a very common task in any program activity. Within python it’s fairly straightforward to do. One area that come up is to remove items from a list while iterating in Python language, this just requires a simple pattern. We give you clear explanations with examples in this article.
There are several solutions for this task such as list comprehension, reverse iteration with remove() function, lambda function with filter() way, and while loop with copy(), append(), pop() functions. They differ from each other by their speed and readability.
The most ineffective method among the above ways is iterating through the list and removing certain items by given condition via remove() function. Because this involves copying the entire list and performing O(n) remove operation to remove certain element which is O(n^2) algorithm.
Method 1: List comprehension
This is the easiest solution for a task. We create a list comprehension and then filter the list to save items from the given condition. So let us see an example to understand the concept. Here we want to delete the list when iterating over it. When all occurrences are not less than 5 then delete. The concept is like this:
somelist = [x for x in somelist if not determine(x)]
So:
list_items = list(range(7))
list_items[:] = [x for x in list_items if x < 5] print(list_items)
Output:
Method 2. Reverse iteration.
Here we iterate values in reverse order which guarantees no elements are skipped. We use reversed() function to reverse a list. We can see an example to understand the concept:
list_items = list(range(7))
for x in reversed(list_items):
if x >= 5:
list_items.remove(x)
print(list_items)
Output:
Method 3. Lambda function.
Lambda function gives a quick solution to this task. Filter() function in python is to accept a function as well as a list as an argument. This function is also to filter out elements from a sequence. Let us look at an example to understand the concept:
list_items = list(range(10))
list_items = list(filter(lambda x: (x < 5), list_items)) print(list_items)
Output:
Method 4. While loop to pop and append items.
Another method is using while loop to iterate through each value in the list. Then we extract them out of the list to items one by one. After that, we check a condition on each value and append them to another list. When the list is ready, we will copy that list to original list and delete that temporary list in order to save memory space.
Here we use a pop() function to remove a specified Python index. If there is no index, it will remove the last item from the list. The append() way is to add an item to the end of the list. Del keyword is to delete the index or the entire list.
Example:
list_items = list(range(7))
# here we create a temporary list in order to store the items that satisfy the criteria
temporary_list = []
# while loop to loop through a list
while list_items:
# variable x holds the items of the list one by one. pop() is used to extract them from the list.
x = list_items.pop()
if x < 5:
# appending the items that meet the criteria to the temporary list
temporary_list.append(x)
# the temporary list stores items in the reverse order. So copy them to list_items using reversed()
list_items = list(reversed(temporary_list.copy()))
# delete the temporary list to save memory
del temporary_list
print(list_items)
Output:
Conclusion
In conclusion, if we want to remove items from a list while iterating, we can use a list comprehension, a reverse iteration, a lambda function and a while loop using pop(), append() functions. Among them, list comprehension is the fastest in many scenarios. However, we can use any of them according to our needs.
Subscribe to our newsletter
Error SendFox Connection: