Summary: To remove items from a list while iterating, use any of the following methods.
- List comprehension,
- Reverse iteration with the
- Lambda Function with the
- While loop with the
Let’s start with defining the exact problem you want to solve.
Problem: Given a list. How to remove items from the list while iterating through them.
A very plain and ineffective approach to our problem could be iterating through the list and removing the required item according to the given condition using the
remove() method. Let us have a look at why using such an approach can be disastrous for our code.
Example: Suppose that we have a list
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] . We want to remove all items less than 5. Now follow the program given below :
li = list(range(10)) for b in li: if b < 5: li.remove(b) print(li)
The ‘Expected Output’ for the above program is
[5, 6, 7, 8, 9]. However, the output that we actually get is completely different from our expectations.
[1, 3, 5, 6, 7, 8, 9]
Now, you might be wondering what just happened? There is no error in the condition defined, then why are we getting an erroneous output. No worries!!! The reason is quite simple and obvious once you have a look at the visual of what is exactly happening to our code.
From the above explanation, we can deduce that the iterator has no idea about a list element being removed and advances to the next item without any hassle. But, as soon as an element is deleted it is replaced by the next element.
For example: In the first iteration, 0 gets deleted based on our condition and is replaced by 1. However, the iterator moves on to the next position which now holds the value 2 instead of 1. This causes the condition of our program to be misinterpreted which results in a wrong output.
Now that we have an overview of the problem definition, let us have a look at the probable solutions to our problem.
First, let’s start with an overview:
Exercise: Run the code. Is the output the same for each method?
Let’s dive deeper into each of the methods.
Method 1: Using List Comprehension
The easiest solution to our problem is to create a list comprehension and then filter the source list to store values based on the given condition. List Comprehensions are an effective way to create new lists from other lists / iterables.
Let us have a look at the following program to understand the concept:
li = list(range(10)) li[:] = [x for x in li if x >= 5] print(li)
[5, 6, 7, 8, 9]
Attention: Avoid using extremely long list comprehensions to make the code user friendly and also avoid code complexities.
Method 2: Reverse Iteration
Another workaround to our problem is a little trick. Instead of iterating through the items normally, you can iterate through the items in reverse order. This will ensure that no elements are skipped. Wondering how?
This is because removing an item in reverse order will only affect those items which have already been handled. A list can be reversed using the
reversed() function. Let us have a look at the following code to understand this concept:
li = list(range(10)) for x in reversed(li): if x < 5: li.remove(x) print(li)
[5, 6, 7, 8, 9]
Method 3: Using a Lambda function
filter() function in python can be used to accept a function and a list as an argument. It can be used to filter out elements from a sequence based on the function.
The following program demonstrates how we can use lambda to iterate through the list based on our condition:
li = list(range(10)) li = list(filter(lambda x: (x >= 5), li)) print(li)
[5, 6, 7, 8, 9]
Method 4: Using a While Loop to Pop and Append Items
Another work-around for our problem can be using a while loop to iterate through each item in the list and extracting them out of the list into a variable one by one. Then check the condition on each item and append them to another list accordingly. Once the resultant list is ready we can copy the newly created list into the original list and then delete the temporary list (to save memory space).
- pop(): the
pop()method is a built-in Python method which is used to remove a specified Python index. If no index is provided then it removes the last item from the list.
- append(): the
append()method is a built-in Python method which is used to add an item to the end of an existing list without affecting the pre-existing items in the list.
- del: the
delkeyword is used to delete a particular index or an entire list.
Now that we have gone through some of the basic terminologies to solve our problem, let us have a look at the following program to understand the usage of the above concept. (Please follow the comments to get a better grip on the code.)
li = list(range(10)) # creating a temporary list to store the items that meet the criteria temp =  # while loop to loop through the list while li: # variable x holds the items of the list one by one. pop() is used to extract them from the list. x = li.pop() if x >= 5: # appending or adding the items that meet the criteria to the temp list temp.append(x) # the temp list stores items in the reverse order. So copy them to li using reversed() li = list(reversed(temp.copy())) # delete the temp list to save memory del temp print(li)
[5, 6, 7, 8, 9]
From the above discussion, we can safely say that to iterate through a given list and remove items according to the given condition we can make use of:
- a list comprehension or
- a reverse iteration or
- a lambda function or
- a general while loop using
A list comprehension is always the best solution when compared to the usage of a filter function because a
map() function in a Python program can be slightly faster in normal scenarios, but when used with a lambda function it is always slower as compared to a list comprehension.
However, you are free to use any option that suits your needs.
I hope you found this article useful and it helps you to iterate through a list and remove the items based on a given condition. Stay tuned for more interesting articles coming up in the future.
Where to Go From Here?
Enough theory. Let’s get some practice!
Coders get paid six figures and more because they can solve problems more effectively using machine intelligence and automation.
To become more successful in coding, solve more real problems for real people. That’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?
You build high-value coding skills by working on practical coding projects!
Do you want to stop learning with toy projects and focus on practical code projects that earn you money and solve real problems for people?
🚀 If your answer is YES!, consider becoming a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.
If you just want to learn about the freelancing opportunity, feel free to watch my free webinar “How to Build Your High-Income Skill Python” and learn how I grew my coding business online and how you can, too—from the comfort of your own home.