• Register
search
Log In
0 votes
27 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.9k points

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]

36.1k points
edited by

Related questions

0 votes
2 answers 17 views
17 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 LizzyM 5.7k points
1 vote
1 answer 13 views
13 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 Gavin 14.9k points
0 votes
1 answer 43 views
43 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 jennygotit 200 points
1 vote
1 answer 4 views
4 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 adamSw 8.3k points
1 vote
1 answer 12 views
12 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 LizzyM 5.7k points