Remove Elements from a List while Iterating

Python: Remove Elements from a List while Iterating

A collection is an ordered list of values. There could be various types of values. A list is a mutable container. This means that existing ones can be added to, deleted from, or changed.

The Python list represents the mathematical concept of a finite sequence. List values are referred to as list items or list elements. The same value may appear multiple times in a list. Each event is regarded as a distinct element.

Example:

Input:

givenlist = ["hello", "this", "is", "this", "BTechGeeks", "this", "python"]

Output:

['hello', 'is', 'BTechGeeks', 'python']

Delete Elements from a List while Iterating

There are several ways to remove elements from the list while iterating some of them are:

Method #1:Using for in loop

To accomplish this, we must first make a copy of the list, and then iterate over that copied list. Then, for each element, we’ll decide whether or not to delete it. If so, use the remove() function to remove that element from the original list.

Below is the implementation:

# given list
givenlist = ["hello", "this", "is", "this", "BTechGeeks", "this", "python"]
# given element which should be deleted
key = "this"
for element in list(givenlist):
    # checking if the element is equal to given key
    if(element == key):
        # using remove to remove element from list
        givenlist.remove(element)
# print the list
print(givenlist)

Output:

['hello', 'is', 'BTechGeeks', 'python']

Method #2:Using List Comprehension

Using list comprehension, we can iterate over the list and choose which elements to keep in the new list. The new list can then be assigned to the same reference variable that was part of the original list.

Below is the implementation:

# given list
givenlist = ["hello", "this", "is", "this", "BTechGeeks", "this", "python"]
# given element which should be deleted
key = "this"
# using list comprehension to remove given element from the list
givenlist = [element for element in givenlist if element != key]
# print the list
print(givenlist)

Output:

['hello', 'is', 'BTechGeeks', 'python']

Method #3:Using filter() function

The Filter() function takes two arguments,

The first is a Lambda function, which can be any function.
The second list is the one from which we want to remove elements.
It iterates through all of the list’s elements, applying the given function to each one. It returns the elements for which the given function returns True during iteration. So we can use the filter() function to iteratively filter elements from a list.

Below is the implementation:

# given list
givenlist = ["hello", "this", "is", "this", "BTechGeeks", "this", "python"]
# given element which should be deleted
key = "this"
# using filter() function to remove given element from the list
givenlist = list(filter(lambda element: element != key, givenlist))
# print the list
print(givenlist)

Output:

['hello', 'is', 'BTechGeeks', 'python']

Related Programs: