| by Arround The Web | No comments

Python Remove all Instances From List

Developers often add or remove data from the data structure according to their needs and “list” is one of them. The repeated values of the list are known as occurrences or instances. Sometimes developers want to eliminate the repeated values from the list to make it clear of the redundant values. For those corresponding purposes, Python has multiple built-in functions.

This article will demonstrate different methods for removing all instances from a list in Python.

How to Remove All Instances From the Python List?

There are multiple functions used to remove all instances from the list in Python. As follows:

Method 1: How to Remove All Instances From the List Using the “remove()” Method in Python?

The “remove()“ method can be used for eliminating all instances from the list. It will remove the particular element from the whole list. To do so, first, initialized a list:

myList = [8, 6, 9, 4, 3, 2, 5, 9]

 

Then, use the “print()” statement to display the declared list elements. After that, specify the particular instance that we want to remove from the whole list. In our case, we want to remove all occurrences of the “9” from the provided list and save them into the variable named “r_instnace”:

print ("Original List" , myList)
r_instance = 9

 

Now, invoke the “remove()” method and pass it to the variable that holds the desired instance of the list. Lastly, apply the “print()” method to display the resultant string:

myList.remove(r_instance)
print("Resultant List" , rList)

 

According to the below-given output, the specified element has been removed from the input list:

Method 2: How to Remove All Instances From the List Using the “filter()” Method in Python?

Another easiest way for removing all instances from the list is by using the “filter()” method. It is quite handy when users want to remove the same element occurrences from an input string. Let’s use it and understand it’s working.

The input string has been declared and initialized in the previous example. Now, apply the “lambda” function inside the “filter()” method on the list to find all occurrences of the provided numbers from the input list, while utilizing the variable “r_instance” value. All values that are filtered from the provided input string would be saved to a new list “rList”. Lastly, call the “print()” method to display the filtered list:

rList = [list(filter(lambda x: x != r_instance, myList))]
print("Resultant List" , rList)

 

Output

Method 3: How to Remove All Instances From the List Using the “List Comprehension” in Python?

The list comprehension method is also utilized for removing all instances from the list. By using this method, users can create a sublist of those elements that meet the specified condition.  List comprehension includes expression, followed by a for loop enclosed within the “[]” square brackets.

Here, we have used the previously specified “myList” input string and the “r_instance” variable. Then, we used the list comprehension method and the “for” loop cycle to match the occurrence. When the provided condition is satisfied, the rest of the values would be saved to the “rList”  list. After that,  invoke the “print()” function to display the resultant list:

rList = [x for x in myList if x != r_instance]
print("Resultant List" , rList)

 

It can be seen that the desired instance of the list has been removed from the resultant list:

That’s all! We have described the different methods for removing all instances from the list in Python.

Conclusion

To remove all instances from the input string in Python, different methods are used, such as the “remove()” method,  “filter()”  method, and “list comprehension” method. In this article, we have illustrated multiple methods for removing all instances from a list in Python.

Share Button

Source: linuxhint.com

Leave a Reply