Python

Python Remove all Instances from List

You have been playing with many data structures to add or remove items or data within them. The list is one of them. The repeated same values are said to be instances or occurrences. Have you ever tried to remove some specific repeated values from a list? If not, then this article is meant for you. Therefore, we will be discussing some methods to remove all the same instances from a list data structure within Python. For this, we will use the Spyder3 python tool.

Method 01: Remove Function

The very first and simplest method to remove all instances from a list is using the “remove()” method in our python code. So, within the Spyder3 project, we initialized a list “List” with some integer-type values. A variable “r” has been defined with the value “2”. The value “2” of variable “r” will be used as a matching occurrence. The print statement is used to print out the original list.

We have been utilizing the “try-except” statement in our code to remove the same occurrences. Within the “try” body, we have used the “while” to continue to run until the end of a list. The remove() method is deleting all the instances of a variable “r” having value “2” from the list. While the except statement is used to continue the program if some value error occurs. After the whole list has been checked out, the left items of a list will be printed out.

List = [2, 4, 6, 8, 0, 2, 3, 3, 5, 2, 6, 3, 2, 8, 10]
r = 1
print (“Before removed” , List)
try:
    while True:
        List.remove(r)
except ValueError:
    pass
print(“After removal” , List)

We have got the two lists upon executing this code, i.e., before removal of instances and after removal of instances.

Method 02: List Comprehension

The ” list comprehension ” method is the next and easy method to remove all the instances from a list is the “list comprehension” method. We have been utilizing the same list here. Also, we have been using the same variable “r” with the same value “2” to be used as an instance for the removal. After printing out the original list, we have utilized the list comprehension method, i.e., used for loop to match the occurrence. After the condition gets satisfied, the left values would be saved to the list “List” and the matched one would be ignored. A newly updated list will be outputted on the console.

List = [2, 4, 6, 8, 0, 2, 3, 3, 5, 2, 6, 3, 2, 8, 10]
r = 1
print (“Before removed” , List)
List = [ v for v in Last if v! = r)
print(“After removal” , List)

The result of this code is the same as we got the result in the first method, i.e., original list and occurrence-free list.

Method 03: Filter Function

The filter() function is quite handy when it comes to removing the same element occurrences from a list. So, we have declared a string-type list with 7 string values in it. After initializing a list, we declared a string variable named “r” having “snow” as a value. This string would be used further as a matching instance. The original string list has been printed out with the help of a print clause. Then, we applied the filter() function on a list while using the “r” variable to check.

If the list value doesn’t match with the variable “r” value, it will filter that particular list value. All the filtered values from the original list would be saved to a list “List”. The updated listed will be displayed after the whole list has been filtered.

List = [‘John’, ‘snow’, ‘kelly’, ‘bryan’, ‘snow’, ‘william’, ‘snow’ ]
r = ‘snow’
print (“Before removed” , List)
List = list( filter (r) . _ne_, List)
print(“After removal” , List)

The output for this code presents the original list and the newly updated and filtered list.

Conclusion

This article contains three simple and well-reputed methods to remove the same instances or occurrences from any python list. This article will be helpful to all our users, and we hope it will make you understand the concept smoothly and quickly.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content