3

A small example. I have two lists with numbers, ra and dec here. I have a third list that also has some numbers, quad here.

What I want to do is to remove those values of ra and dec that are in quad.

>>> ra = [1,1,1,2,3,4,5,6,7,8]
>>> dec = [1,2,3,4,5,6,7,7,7,7]
>>> quad = [1,2,3,1,2,3]
>>> new_ra = []
>>> new_dec = []
>>> for a,b in zip(ra,dec):
        if ((a not in quad) & (b not in quad)):
            new_ra.append(a)
            new_dec.append(b)

So here you would expect:

new_ra = [4,5,6,7,8]

and

new_dec = [4,5,6,7,7,7]

How ever, I get:

new_ra = [4,5,6,7,8] 

as expected, BUT,

new_dec = [6,7,7,7,7]

Why is this so? What is wrong with my loop?

P.S. I am following the same method as in THIS QUESTION, but my second list does not give me the proper answer.

5
  • By the way & is bitwise AND, you want to use the keyword and for logical AND Jul 16, 2015 at 14:35
  • I am following the same method, but my second list does not give me what I want!!! What is wrong here??
    – Srivatsan
    Jul 16, 2015 at 14:35
  • When b is equal to 4 a is equal to 2. You add elements to new_ra and new_dec lists only when a is not in quad and b is not in quad. It means, that 4 will never be added to new_dec
    – Konstantin
    Jul 16, 2015 at 14:38
  • @CoryKramer: I should say that and does not make a difference here and I get the same results
    – Srivatsan
    Jul 16, 2015 at 14:38
  • @Alik: So how do I achieve it if I want it for remove for both the lists?
    – Srivatsan
    Jul 16, 2015 at 14:40

1 Answer 1

5

Why don't you use a simple list comprehension?

new_ra = [v for v in ra if v not in quad]
new_dec = [v for v in dec if v not in quad]
1
  • quad could also be converted into a set to remove any possible duplicates. Jul 16, 2015 at 14:47

Not the answer you're looking for? Browse other questions tagged or ask your own question.