Python : How to Remove Multiple Elements from List ?

In Python, a list is used to store the sequence of different types of data. Python lists are mutable, which means we can change their elements after they’ve been formed. However, Python has six data types that can be used to store sequences, with the list being the most common and accurate.

A list can be described as a set of different types of values or objects. The comma (,) separates the things in the list, which are enclosed in square brackets [].

Remove multiple items from the list

The following is a list of possible python methods to remove multiple elements from the list:

Method #1:Using remove() function while iterating through the list

The remove() method removes the first matching element from the list (which is passed as an argument).

remove() parameters:

  • The remove() method accepts a single argument and removes it from the list.
  • If the element does not exist, a ValueError: list.remove(x): x not in list exception is thrown.

Let us remove all elements which are divisible by 2.

Traverse the list and remove the element if it is divisible by 2.

Below is the implementation:

# function which remove multiple items from the list
def removeMultiple(givenlist):
    # Traverse the list
    for element in list(givenlist):
        # Checking the given condition
        if(element % 2 == 0):
            # remove the element from the given list
            givenlist.remove(element)
    # return the given list
    return(givenlist)


# Driver code
# Given list
givenlist = [1, 2, 3, 4, 5, 6, 7, 8]
# passing the list to removeMultiple function
print(removeMultiple(givenlist))

Output:

[1, 3, 5, 7]

Method #2:Using del keyword and slicing

Another way to delete an element from a list is to use its index in the del statement. It differs from the pop() function in that it does not return the removed element. Unlike the slicing feature, this does not generate a new list.

If we want to remove multiple elements from a list based on an index range, we can use the del keyword.

Suppose we want to delete elements from 2 to 5 indices we can use slicing and del keyword to do that.

It will remove the elements from index1 to index2 – 1 from the list.

Below is the implementation:

# function which remove multiple items from the list
def removeMultiple(givenlist):
    # using del keyword and removing indices from 2 to 5
    # increment the right index with 1 to remove it
    del givenlist[2:6]
    # return the given list
    return(givenlist)


# Driver code
# Given list
givenlist = [1, 2, 3, 4, 5, 6, 7, 8]
# passing the list to removeMultiple function
print(removeMultiple(givenlist))

Output:

[1, 2, 7, 8]

Method #3: Using List Comprehension

Python’s syntax for deriving one list from another is concise. These expressions are known as list comprehensions. Python list comprehensions make it extremely simple to apply a function or filter to a list of items. List comprehensions can be very useful when used correctly, but they can also be very unreadable if used incorrectly.

Let us remove all elements which are divisible by 2.

We can achieve that easily by list comprehension as given below:

# function which remove multiple items from the list
def removeMultiple(givenlist):
    # using list comprehension
    givenlist = [element for element in givenlist if element % 2 != 0]
    # return the given list
    return(givenlist)


# Driver code
# Given list
givenlist = [1, 2, 3, 4, 5, 6, 7, 8]
# passing the list to removeMultiple function
print(removeMultiple(givenlist))

Output:

[1, 3, 5, 7]

Related Programs: