Reputation: 1445
Using python 2.7.4
Lets say I have a list
list = ['abc', 'def']
I want to find if it contains certain things. So I try:
[IN:] 'abc' in list
[OUT:] True
[IN:] 'def' in list
[OUT:] True
[IN:] 'abc' and 'def' in list
[OUT:] True
But when I list.pop(0) and repeat that last test:
[IN:] 'abc' and 'def in list
[OUT:] True
Even though:
list = ['def']
Anybody know why?
Upvotes: 1
Views: 681
Reputation: 251186
That's because:
abc' and 'def' in list
is equivalent to:
('abc') and ('def' in list) #Non-empty string is always True
Use 'abc' in list and 'def' in list
or for multiple items you can also use all()
all(x in list for x in ('abc','def'))
Don't use list
as a variable name, it's a built-in type.
Upvotes: 5