How To Remove Items From A List While Iterating?

Summary: To remove items from a list while iterating, use a List Comprehension or a Reverse Iteration Technique along with the remove() method or a Lambda Function with the filter() method. You can also use a While loop with the copy(), pop() and append() functions to remove the items while iterating through the list.

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.

Actual Output:

[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.

Solutions:

Table of Contents

Method 1: Using A 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)

Output:

[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)

Output:

[5, 6, 7, 8, 9]

Method 3: Using A Lambda function

Using a lambda function might be tricky but it often provides quick solutions to our problems. The power of the lambda function can be leveraged in this case too.

#NOTE

A 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)

Output:

[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).

#NOTE

  • 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 del keyword 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)

Output:

[5, 6, 7, 8, 9]

Conclusion

From the above discussion, we can safely say that we can make use of a list comprehension or a reverse iteration or a lambda function or a general while loop using pop() and append() functions to iterate through a given list and remove items according to the given condition. A list comprehension is always the best solution when compared to the usage of a filter function because a filter() or 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!

To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And 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?

Practice projects is how you sharpen your saw in coding!

Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?

Then become 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.

Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home.

Join the free webinar now!

Author: admin

Leave a Reply

Your email address will not be published.