0

I have two lists: a = [10.0,20.0] and b = [1.0,10.0,15.0,20.0,30.0,100.0]. How can I remove from list b all the elements between 10.0 and 20.0? Here is what I tried:

c = [b[y] for y in range(len(b)) if (b[y] < a[0] or b[y] > a[1])]

I expect to get c = [1.0, 30.0, 100.0], but I get c = [1.0,10.0,15.0,20.0,30.0,100.0].

How can I exclude components from a list that are in a certain range by using only list comprehension?

Gholamali Irani
  • 4,391
  • 6
  • 28
  • 59
Edwin
  • 55
  • 6
  • 2
    I got the expected result, you sure that you don't do something like `c=b` somewhere and that is the real problem? – Copperfield Jan 15 '18 at 01:52

4 Answers4

4

You can simplify by iterating b's elements directly, but your original code works for me, too:

a = [10.0, 20.0]
b = [1.0, 10.0, 15.0, 20.0, 30.0, 100.0]

c = [x for x in b if x < a[0] or x > a[1]]
# [1.0, 30.0, 100.0]

# Your version:
c = [b[y] for y in range(len(b)) if (b[y] < a[0] or b[y] > a[1])]
# [1.0, 30.0, 100.0]
user2390182
  • 72,016
  • 6
  • 67
  • 89
  • Thanks, this exactly answered my question, but I asked the wrong question because I wanted to show you guys a simplified version of the issue. I've posted a new more detailed question here, this time I've checked that my solution fails :) : https://stackoverflow.com/questions/48257451/unable-to-exclude-items-from-a-list-in-a-while-loop-that-are-in-a-certain-rang – Edwin Jan 15 '18 at 05:41
2

Think from the opposite, only includes components that are in a certain range, like this:

c = [y for y in b if (y < a[0] or y > a[1])]

Hou Lu
  • 3,012
  • 2
  • 16
  • 23
2

You can use filter:

a = [10.0,20.0]
b = [1.0,10.0,15.0,20.0,30.0,100.0]
new_a = list(filter(lambda x:x < a[0] or x > a[-1], b))

Output:

[1.0, 30.0, 100.0]
Ajax1234
  • 69,937
  • 8
  • 61
  • 102
-2

Filter function will do this for you :

c= filter(lambda x: x<10.0 or x>20.0,b)
DARK_C0D3R
  • 2,075
  • 16
  • 21
  • 1
    1. Pls, do not use filter and map with lambda, it is just ugly. 2. Using an integer range for equality tests with floats is unpredictable. 3. The contains-check must iterate the range for every element and is therefore bad performance-wise. – user2390182 Jan 15 '18 at 01:42
  • This doesn't even work if `x` is not a whole number. – cs95 Jan 15 '18 at 01:42
  • @cᴏʟᴅsᴘᴇᴇᴅ Definitely not in the general case; for the OP's data, one might get lucky, but still... – user2390182 Jan 15 '18 at 01:44
  • @coldspeed , it works for negative values too. – DARK_C0D3R Jan 15 '18 at 01:44
  • Most probably `not in range(10,20)` is not what you think it is. – DYZ Jan 15 '18 at 01:45
  • @TheVishal Not sure what I'm supposed to glean from that nugget of information. Your code does not work, for say, `12.5`. – cs95 Jan 15 '18 at 01:46
  • @coldspeed , thanks for pointing out sometimes answering other's question clears yours too :) – DARK_C0D3R Jan 15 '18 at 01:55
  • 1
    Unfortunately, your answer is now identical to: https://stackoverflow.com/a/48255963/4909087 – cs95 Jan 15 '18 at 02:05