Python program to remove all occurrence of a value from a list

Python: remove all occurrence of a value from a list :

In this tutorial, we will learn how to remove all occurrence of a value from a list in python. For example, if the list is [1,1,2] and if we will remove all occurrence of 1, it will become [2]. I will show you two different ways to solve this problem. Let’s take a look :

Method 1: Using a while loop :

Using one while loop, this program can be solved easily. The main idea is to run a while loop continuously, check for any assistance and remove that value on each iteration. Means, for the example above, we will check if the value 1 exists or not in the list on each iteration of the while loop. If it exists, we will remove it, and if not, we will exit the program. The program will look like as below :

#1
list_size = int(input("How many numbers you want to store in the list : "))

#2
user_list = []

#3
for i in range(list_size ):
    user_list.append(int(input("Enter element %d : "%(i+1))))

#4
print("This is your list : ",user_list)

#5
flag = int(input("Enter the number you want to remove from this list : "))

#6
while flag in user_list :
    user_list.remove(flag)

#7
print("Final list : ",user_list)

Explanation :

The commented numbers in the above program denote the step number below :

1. Ask the user to enter the size of the list. Read it and store it in the list_size variable.
2. Create one empty list user_list to store all user input numbers.
3. Run one for loop. Ask the user to enter values for the list. Read it and append to the list.
4. Print the user input list to the user.
5. Ask the user to enter the number to remove from the list. Store it in the flag variable.
6. Run a while loop and check continuously if the flag exists in the user_list or not. If yes, remove it from the list. The remove() method removes the first occurrence of a value from a list. So, we need to call it multiple times if that number has multiple occurrences in that list.
7. Print out the final list to the user.

Sample Output :

How many numbers you want to store in the list : 5
Enter element 1 : 3
Enter element 2 : 1
Enter element 3 : 2
Enter element 4 : 3
Enter element 5 : 3
This is your list :  [3, 1, 2, 3, 3]
Enter the number you want to remove from this list : 3
Final list :  [1, 2]

python remove occurance from a listpython remove occurance from a list
The main problem with this approach is that we are checking for the existence of a value multiple times in the list. So, we are scanning the list multiple times. We can also optimize it to scan only one-time using list comprehension like below.

Method 2: Using list comprehension :

We can scan the whole list only one time to remove all occurrence of a specific value.

list_size = int(input("How many numbers you want to store in the list : "))

user_list = []

for i in range(list_size ):
    user_list.append(int(input("Enter element %d : "%(i+1))))

print("This is your list : ",user_list)

flag = int(input("Enter the number you want to remove from this list : "))

final_list = [item for item in user_list if item != flag]

print("Final list : ",final_list)

The main benefit of this method than the previous one is that we need to scan the list only for one time. If you have a list of very large size, you should use this method to optimize your runtime. The only problem is it will create one different list unlike the previous one.
python remove occurrence from a listpython remove occurrence from a list
As you can see the output is same as the previous one.

Conclusion :

We have seen two different ways to remove all occurrence of a value in a list in python. It totally depends on you which one you want to use. The second method is more preferred to use if you are dealing with a large size of the list and the first one is for changing the original list itself. Go through the programs we have explained above and drop a comment below if you have any queries.

You might also like :

Leave a Reply