Remove all instances of element from list in Python

In this article, we will see different ways to remove all occurrences or instances of a given element from the list in Python.

Let’s see the example:
Example:

Now, let’s see different methods:

Using remove() method of list with for loop

remove() method is used to remove the 1st occurrence of given element from the list. So here we are looping through the list, and whenever we find the given element, then we will remove that element from the list using remove() method.
Here is Python code:

Output:

Using pop() method of list

pop() method is used to remove the 1st occurrence of given index element from the list. So here we are looping through the list, and whenever we find the given element, then we will remove that element from the list using pop() method by passing its corresponding index.

Here is Python code:

Output:

Using list-comprehension

Here, we are creating a new list by adding elements that are not matching with the given element using list-comprehension.
This is recommended solution to use, and here we are creating a sublist that satisfies certain conditions. In this scenario, if item is not matching with the given element

Here is Python code:

Output:

Using filter()

We can also use inbuilt filter(function,iterable) method which can return an itertor from elements of the list for which function returns true.
Here is Python code:

Using remove() method of list with while loop

We can also use remove() method with while loop rather than for loop as seen in first approach.

We will iterate the elements using while loop and when we find the element in the list, we will remove it.
Here is Python code:

Output:

That’s all about how to remove all occurrences or instances of a given element from the list in Python.


import_contacts

You may also like:

Related Posts

  • Python copy file to another directory
    17 January

    How to copy file to another directory in Python

    Table of ContentsWays to copy file to another directoy in PythonUsing file handlingUsing the shutil libraryUsing the pathlib libraryUsing the os moduleUsing the subprocess moduleConclusion In this article, we will see different ways to copy file to another directory in Python. We can read and write files in Python. We can also work with paths […]

  • Check if variable is String in Python
    13 January

    Check if variable is String in python

    Table of ContentsHow to check if a given variable is of the string type in Python?Using the isinstance() function.Using the type() function.Check if function parameter is String In this post, we will see what is a string in Python and how to check whether a given variable is a string or not. There are many […]

  • Count the number of characters in a String in Python
    13 January

    Count number of characters in a string in python

    Table of ContentsWays to count the number of characters in a string in PythonUsing the len() functionUsing the for loopUsing the collections.Counter classConclusion In this post, we will see how to count number of characters in a String in Python. We can think of strings as a collection of characters, with every character at a […]

  • Pandas apply function to column
    12 January

    Pandas apply function to column

    Table of ContentsHow do I apply function to column in pandas?Using dataframe.apply() functionUsing lambda function along with the apply() functionUsing dataframe.transform() functionUsing map() functionUsing NumPy.square() function We make use of the Pandas dataframe to store data in an organized and tabular manner. Sometimes there, is a need to apply a function over a specific column […]

  • Copy file Python
    12 January

    How to Copy File in Python?

    Table of ContentsCopy file using the shutil module in PythonCopy file using the copyfileobj() function in PythonCopy file using the copyfile() function in PythonCopy file using the copy() function in PythonCopy file using the copy2() function in PythonCopy file using the operating system command in PythonCopy file using the os module in PythonCopy file using […]

  • What is percent in python
    11 January

    What is % in Python?

    Table of ContentsWhat is % Operator in Python?The % Operator with strings in pythonThe % Operator as a placeholder for variables  in pythonThe % Operator as format specifiers in pythonConclusion In this article, we will cover what is % in Python and what are different ways to use % in Python. In python, there are […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.