2

I am new to python and was wondering if someone could help me out with this. I am trying to see if the elements in b are in a. This is my attempt. Currently I am not getting any output. Any help would be appreciated, thank you!

a = [1]
b = [1,2,3,4,5,6,7]

for each in b:
    if each not in a == True:
        print(each + "is not in a")
2
  • You should do if each not in a: print(each + "is not in a") Commented Sep 17, 2012 at 17:26
  • 2
    Apart from the correct answer given by Martijn you will also need to change the print: you cannot add a number and a string. Either convert the number to a string or use string formatting. Commented Sep 17, 2012 at 17:29

8 Answers 8

4

You are testing two different things, and the outcome is False; Python is chaining the operators, effectively testing if (each is in a) and (a == True):

>>> 'a' in ['a'] == True
False
>>> ('a' in ['a']) and (['a'] == True)
False
>>> ('a' in ['a']) == True
True

You never need to test for True on an if statement anyway:

if each not in a:

is enough.

Sign up to request clarification or add additional context in comments.

7 Comments

I'd add that, if you actually want to see if something is True(the object True, not the truth value), then you have to use the is operator.
@Bakuriu: Let's not muddle the waters here just yet. One step at a time!
@MartijnPieters: If Python parses if each in a == True as if each in (a == True), then why isn't a TypeError: argument of type 'bool' is not iterable raised? Just curious...
@unutbu -- Yeah, It doesn't parse that way. Operator chaining is actually (probably) being used here ... (although I'm a little perplexed as to how)
It is testing each in a and a == True
|
2

You should be able to just say:

if each not in a:
   print ("%d is not in a" % each)

Your actual expression is using operator chaining:

if a > b > c:

parses as:

if (a > b) and (b > c):

in python. which means your expression is actually being parsed as:

if (each not in a) and (a == True):

but a == True will always return False, so that if block will never execute.

6 Comments

Would this work in python 3.x, given that each is of type int?
@inspectorG4dget -- I don't see how that would matter. Each is an int, and you look to see if it is in the list a...
I was referring to the print(e + " is not in the list"). I was asking if the concatenation would work between a str and an int
@inspectorG4dget -- Oh, that doesn't work on py2k or py3k. I was just copying OP's code assuming that part would work. I updated accordingly.
@inspectorG4dget -- If anything, py3k is more strict than py2k. For example, 1 > "a" is legitimate in py2k, but a TypeError in py3k.
|
1
a = [1,2,3]
b = [1,2,3,4,5,6,7]
c = [7,8,9]

print set(a) <= set(b) #all elements of a are in b
print set(c) <= set(b) #all elements of c are in b

Comments

1

It is better to see the difference between B and A

 set(b).difference(set(a))

1 Comment

Or set(b).difference(a). No need to cast the argument to set.
0

You don't need ==True. Just: if each not in a:

Comments

0

This is really easy using sets:

a = [1]
b = [1, 2]

only_in_b = set(b) - set(a)
# set([2])

Comments

0

Another way:

for bb in b:
    try:
        a.index(bb)
    except:
        print 'value is not in the list: ' + str(bb)

Comments

0

I would like to add that if the two lists are large. This is not the best way to do it. Your algorithm is O(n^2). The best way is to traverse a, adding the elements as keys to a dictionary. Afterwards, traverse the second list, checking if the elements are already in the dictionary, this instead is an O(n) algorithm.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.