0

I want to remove all occurences of two values in a list using one line. I have tried the filter + lambda method and list comprehension, but it's not working the way I would expect.

Something like:

s2 = [0, 0, 3, 11, 14, 17, 18, 18]
# I want to remove all 0 and 18 from s2 list
# i.e. I want output: s2_filter = [3, 11, 14, 17]

s2_filter = filter (lambda a: a != 0 or 18, s2)
>>> s2 = [0, 0, 3, 11, 14, 17, 18, 18] # not what I'm expecting

s2_filter = [i for i in s2 if i != 0 or 18]
>>> s2 = [0, 0, 3, 11, 14, 17, 18, 18] # not what I'm expecting

I guess there is something wrong with the or but I cannot figure out how I should write this correctly.

Can you help?

Chanda Korat
  • 2,453
  • 2
  • 19
  • 23
francoiskroll
  • 1,026
  • 3
  • 13
  • 24

1 Answers1

0

You can just filter over if i is not in a non desirable list:

s2 = [0, 0, 3, 11, 14, 17, 18, 18]
s2_filter = [i for i in s2 if i not in [0, 18]]
s2_filter
[3, 11, 14, 17]
Netwave
  • 40,134
  • 6
  • 50
  • 93