Do you know how to remove items from a list while iterating? If not, do not worry because I am here to make you understand the same. Here, we will be seeing quite an ample number of examples to get clearer in this regard. So, let’s get started without waiting any more.

numbers = (20, 24, 26, 29, 31, 33, 37, 38)

Now, suppose that we need to delete all odd numbers from the list. Then, what do we do? We will need to write a loop to iterate over the list of numbers and then, give the condition to check for odd numbers and thereafter, remove them.

```numbers = (20, 24, 26, 29, 31, 33, 37, 38)
for x in numbers :
if x%2 == 0 :
continue
else :
numbers.remove(x)
print(numbers)```
```# Output :
(29, 31, 33, 37)```

Now, that we have seen one method to do this thing. Let us see further methods to do so.

#### Removing elements from list using list comprehension

We can simply go with iterating over a list; and thereafter select the various elements that we want to display based on a certain condition using a list comprehension. In this example, we will see how we do it in the same list without making a copy of it; or without creating a new list.

```numbers = (20, 24, 26, 29, 31, 33, 37, 38)
numbers = (num for num in numbers if num% 2 != 0)
print(numbers)```
```# Output :
(29, 31, 33, 37)```

So, we see that we can easily remove items from a list while iterating using list comprehension. In this example, we see how we can do the task of implementing the list comprehension in the same list; i.e. without creating a new list which generally happens in usual cases.

## Using filter() to remove elements from a list while iterating

To start with, filter() takes in two parameters; the first parameter is a function; and the second parameter is the list from which to perform the desired operation on the elements.

What does filter() do? It will iterate over every element in the list; and then apply the desired function on the elements of the list. Thereafter, it returns the elements for which the function holds true. Therefore, we can use the filter() function to remove elements from a list while iterating. Let’s see with the same list of numbers.

```numbers = (20, 24, 26, 29, 31, 33, 37, 38)
numbers = list(filter(lambda num : num%2 != 0, numbers))
print(numbers)```
```# Output :
(29, 31, 33, 37)```

## Using while loop to pop and append elements

We can remove the elements from a list while iterating over it; by employing the use of a while loop to pep and then append elements. By using the while loop we iterate over each element and then extract the elements one by one. Thereafter, it checks the conditions and appends them to a new list. Thus, let us proceed.

Before, we go with the example let us learn about pop() and append(). The pop() is a method, that we can use to remove an element at a specific index.

The append() method adds an item to an existing list without making an alteration in the pre-existing elements.

```numbers = list(20, 24, 26, 29, 31, 33, 37, 38)
# creating an empty temporary list "odd" to append the elements
odd = []

while numbers :
num = numbers.pop()
if num%2 != 0 :
odd.append(num)

# now we need to reverse the list of elements because the odd list contains reversed elements since, pop removes the elements starting from the last index
numbers = list(reversed(odd.copy()))

print(numbers)```
```# Output :
(29, 31, 33, 37)```

## SUMMING UP !!

So, now we know that we have various methods that we can utilize to remove elements from a given list; while iterating over it. Thus, we can safely say, that we can make use of list comprehension, reverse iteration; filter(), lambda(), while loop and the remove function. In my opinion, the list comprehension is the most suitable method. However, it is up to you to now choose the option that suits you. Hoping that this article was a great insight for you. Until next time, see-ya !! Do not forget to put out your comments though.

Categorized in: