• Register
0 votes
75 views

Problem :

How to remove all occurrences of a value from a list?

I tried in Python remove() will remove the first occurrence of value in a list. How to remove all occurences?

6 5 3
6,930 points

Please log in or register to answer this question.

1 Answer

0 votes

Solution : 

Functional Approach:

2.x

>>> x = [1,2,3,2,2,2,3,4]
>>> filter(lambda a: a != 2, x)
[1, 3, 3, 4]

3.x

>>> x = [1,2,3,2,2,2,3,4]
>>> list(filter((2).__ne__, x))
[1, 3, 3, 4]

OR

>>> x = [1,2,3,2,2,2,3,4]
>>> list(filter(lambda a: a != 2, x))
[1, 3, 3, 4]

9 7 4
38,600 points

Related questions

0 votes
1 answer 7 views
7 views
Problem: How to remove all occurrences of a value from a list?
asked Apr 16 sumaiya simi 43.9k points
0 votes
2 answers 50 views
50 views
Problem: I am a newbee in python programming. I tried to remove all occurrences of a character in a list python. So I wrote a code: Nums = [11, 22, 11, 33, 11, 44, 11, 55] a = 11 print ("Original Numbers:") print (Nums) i=0 length = len(Nums) while(i< ... line 8, in <module> if(Nums[i]==a): IndexError: list index out of range Why I am getting this output but not the actual result. Please help
asked Mar 22, 2020 LizzyM 6.1k points
0 votes
1 answer 9 views
9 views
Problem: Please offer me an answer to the problem >Which method is used to remove all the elements from a list?
asked 5 days ago Danyal Sjoerd 6.8k points
0 votes
1 answer 23 views
23 views
Problem: have a list of lists in Python: k = [[1, 2], [4], [5, 6, 2], [1, 2], [3], [4]] And I want to remove duplicate elements from it. Was if it a normal list not of lists I could used set. But unfortunate that list is not hashable and can't ... [3], [4]] I don't care about preserve order. Note: this question is similar but not quite what I need. Searched SO but didn't find exact duplicate.
asked Feb 19 Mashhoodch 13k points
0 votes
1 answer 3 views
3 views
Problem: PLease help me writing a python program to remove duplicates from a list. Thank you.
asked 2 days ago Junia Phoebe 15.3k points
0 votes
1 answer 8 views
8 views
Problem: how to I Solve this: remove a list from a list python HELP! I need help understanding this: remove a list from a list python Can someone please help solve this, I am new in python(). Someone have idea to solve this thanks!
asked Mar 10 Mashhoodch 13k points
0 votes
1 answer 6 views
6 views
Problem: I need someone&rsquo;s assistance to solve my problem .. How to remove element from list in python
asked May 5 Ifra 37.2k points
0 votes
1 answer 6 views
6 views
Problem: Is there any difference between del, remove, and pop methods to remove an element from a list?
asked Apr 16 sumaiya simi 43.9k points
0 votes
1 answer 6 views
6 views
Problem: I am a beginner. My teacher has given me an assignment in which there is a list with the list items having a \n. I am asked to remove that newline character from the list and print the results. As I have mentioned earlier that I am a beginner, I am facing ... list, given by the teacher: lst = [&lsquo;1\n&rsquo;, &lsquo;2\n&rsquo;, &lsquo;3\n&rsquo;, &lsquo;4\n&rsquo;, &lsquo;5\n&rsquo;]
asked Apr 5 Code Learner 9.9k points
0 votes
1 answer 6 views
6 views
Problem: how to I Solve this: python remove item from list by value HELP! I need help understanding this: python remove item from list by value Can someone please help solve this, I am new in python(). Someone have idea to solve this thanks!
asked Mar 7 Mashhoodch 13k points