• Register
0 votes
59 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
2 answers 37 views
37 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 5 views
5 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 6 days ago Mashhoodch 9.1k points
1 vote
1 answer 45 views
45 views
Problem: Recently I started learning python and just about to know more about list and tuple in python. I am a little bit known to the list and tuple and I can add or subtracts elements in a list. I wonder how to multiply all elements in a list python? I tried it in the adding process but it didn&rsquo;t work for me. Is it doable? Could anybody please show me the way to do it?
asked Apr 3, 2020 Gavin 15.3k points
0 votes
1 answer 4 views
4 views
Problem: case (in which it does not exist) shows the following error: Traceback (most recent call last): File "D:\zjm_code\a.py", line 6, in <module> b = a.index(6) ValueError: list.index(x): x not in list
asked 4 days ago Mashhoodch 9.1k points
0 votes
1 answer 7 views
7 views
Problem: How do I remove an element from a list by index in Python? I found the list.remove method, but say I want to remove the last element, how do I do this? It seems like the default remove searches the list, but I don't want any search to be performed.
asked 6 days ago Mashhoodch 9.1k points
1 vote
1 answer 29 views
29 views
Problem: Hello guys, I found python list is fun. I was doing some operation with a python list. Let me say, I have a python list looks: is_a_list = [1, 5, 3, 6, 3, 5, 6, 1] with mixed of duplicate values in it. Now, I am willing to remove all the duplicate elements from the list. How do you do that in an easiest and efficient way? I am eagerly waiting for your answers. Thanks.
asked Jun 25, 2020 adamSw 11.3k points
1 vote
1 answer 32 views
32 views
Problem : I have started learning python a few days ago . So I am doing my practice and I am getting cofused with some little things. Supose I have a list that is: listOfnumbers = [16, 22, 96, 15, 4, 39, 56, 40, 13, 22, 15] So what should I have to do to remove element from list python by index
asked Apr 3, 2020 LizzyM 6.1k points
1 vote
1 answer 29 views
29 views
Problem: Can anyone tell me the best way to remove duplicates from list keep order in python. Thank you.
asked Mar 28, 2020 LizzyM 6.1k points
0 votes
1 answer 96 views
96 views
Problem: What is the Python equivalent of Perl's chomp function, which removes the last character of a string if it is a newline?
asked Feb 21, 2020 jennygotit 200 points
0 votes
2 answers 454 views
454 views
Problem : Python Program to Calculate the Average of Numbers in a Given List
asked Oct 21, 2019 peterlaw 6.9k points