Back

Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
in Python by (47.6k points)

In Python, remove() will remove the first occurrence of a value in a list.

How to remove all occurrences of value from a list?

This is what I have in mind:

>>> remove_values_from_list([1, 2, 3, 4, 2, 2, 3], 2)

[1, 3, 4, 3]

1 Answer

0 votes
by (106k points)

If you want to remove all occurrences of value from a list then you can follow the following methods:-

If you are a Python 2.x user then you can use the filter() function to do your task:-

>>> x = [1,2,3,2,2,2,3,4]

>>> filter(lambda a: a != 2, x)

image

If you are a Python 3.x user then you can use the list(filter()) or lambda function to do your task:-

>>> x = [1,2,3,2,2,2,3,4]

>>> list(filter((2).__ne__, x))

image

OR

>>> x = [1,2,3,2,2,2,3,4]

>>> list(filter(lambda a: a != 2, x))

image

Related questions

0 votes
1 answer
0 votes
1 answer
+2 votes
3 answers
Welcome to Intellipaat Community. Get your technical queries answered by top developers!

28.4k questions

29.7k answers

500 comments

94.2k users

Browse Categories

...