| by Arround The Web | No comments

Python Remove all Instances From List

In Python, the “Lists” are data structures that can store and manipulate items/elements or data. Sometimes, to remove unwanted data or to reduce the size and memory, we may want to remove some items that appear more than once in a list. These repeated items are called instances or occurrences.

This post presents how to remove all instances from a list.

How to Remove/Delete All Instances From Python List?

The following methods are utilized to eliminate all the occurrences/instances from the Python list:

Method 1: Remove All Instances From Python List Using “List Comprehension”

The “List Comprehension” retrieves a new list by utilizing the existing list element. This approach is used to remove all the instances from the Python list.

Example

The below code block uses the “List Comprehension” approach to remove all the instances of the specified items from the input list:

list_value = [45, 22, 43, 22, 44, 22]
print("Given List: ", list_value)
output = [i for i in list_value if i != 22]
print("\nList After Removing All the Instances: ", output)

 
In the above code, the “List Comprehension” approach is used combined with the “Not Equal (!=)” operator to eliminate all the entries of a particular element from the given list.

Output


The selected items with multiple instances have been removed appropriately.

Method 2: Remove All Instances From Python List Using “filter()” Function

The “filter()” function is used to extract items/elements from an iterable that satisfies a specified condition. The function retrieves iterable containing items that meet a given condition when a filtering function is applied to an iterable.

Syntax

filter(function, iterable)

 
In the above syntax, the “function” is a function that returns a boolean value, and “iterable” is any object that can be iterated over.

Example

This code is intended to eliminate all occurrences/instances from the Python list:

list_value = [45, 22, 43, 22, 44, 22]
print("Given List: ", list_value)
output = list(filter(lambda val: val !=  22, list_value))
print("\nList After Removing All the Instances: ", output)

 
According to the above code:

    • The “filter()” function applies the lambda function to each of the element values of the given list and returns a new iterable (filter object) that does not contain multiple instances of the specified value.
    • The “list()” function is then utilized to transform the iterable into an updated list.

Output


There are no longer any items with multiple instances in the above output.

Method 3: Remove All Instances From Python List Using “list.remove()” Method

The “list.remove()” method eliminates the first instance of the element/item with the specified value from a list. This method is used along with the “for” loop to remove all the instances from the Python list.

Syntax

list.remove(x)

 
In the above syntax, “x” is the item that needs to be deleted.

Example

The below code removes/deletes all the instances from the Python list:

list_value = [45, 22, 43, 22, 44, 22]
print("Given List: ", list_value)
value = 22
for i in list_value:
    if(i==value):
        list_value.remove(value)
print("\nList After Removing All the Instances: ",list_value)

 
Based on the above code:

    • The “list” and the element value that needs to be removed from the list are initialized, respectively.
    • The “for” loop is combined with the “if/else” statement to iterate through the list and remove multiple instances of the desired element from the list using the “list.remove()” method.

Output


Here, it can be seen that we have removed the items with multiple instances successfully.

Conclusion

The “List Comprehension”, the “filter()” function, or the “list.remove()” method is utilized to eliminate all instances from the specified Python list. The “List Comprehension” is implemented by utilizing the “!=” operator to remove all the instances from the list. Similarly, the “filter()” function or “list.remove()” methods can also be used to eliminate multiple instances from a list. This Python guide presented multiple ways to remove all the instances from the list using appropriate examples.

Share Button

Source: linuxhint.com

Leave a Reply