This question already has an answer here:
-
How to test multiple variables against a value?
21 answers
I am trying to add multiple 'or' clauses to a python if statement using list comprehension. My code is shown below. I would like to keep the list comprehension. In terms of pseudocode, the logic would simply be:
Alive_Beatles = each name that contains '(Beatle)' and either ('Paul', 'Yoko' or 'Ringo')
The code only returns Paul and skips Ringo and Yoko.
Names = ["John Lennon (Beatle)", "Paul McCartney (Beatle)", "Ringo Starr (Beatle)", "Yoko Ono (Beatle)", "Mick Jagger (Rolling Stone)", "Brian Jones (Rolling Stone)", "Alex Jones (na)", "Adam Smith (na)"]
Alive_Beatles = [n for n in Names if ("Beatle" and ("Paul" or "Ringo" or "Yoko")) in n]
print Alive_Beatles
You need to test each name explicitly if it's in n
:
[n for n in Names if ("Beatle" in n and ("Paul" in n or "Ringo" in n or "Yoko" in n))]
Otherwise the and
and or
use the truth value of you search strings (and each non-empty string is always True) and finally tests if Paul in n
(the first truth value of the or
s).
The documentation explicitly mentions this:
4.2. Boolean Operations — and, or, not
These are the Boolean operations, ordered by ascending priority:
Operation Result Notes
x or y if x is false, then y, else x (1)
x and y if x is false, then x, else y (2)
not x if x is false, then True, else False (3)
Notes:
(1) This is a short-circuit operator, so it only evaluates the second argument if the first one is false.
(2) This is a short-circuit operator, so it only evaluates the second argument if the first one is true.
(3) not has a lower priority than non-Boolean operators, so not a == b is interpreted as not (a == b), and a == not b is a syntax error.
So "Beatle" and (...)
evaluates according to (2) to the second argument because "Beatle"
is truthy and according to (1) it evaluates to the first argument of the chained or
s: "Paul"
because it's also truthy.
This doesn't do what you expect because the expression
("Paul" or "Ringo" or "Yoko")
evaluates to "Paul"
. Type it in at an interpreter prompt to confirm this.
And even that only seems to work because
("Beatle" and ("Paul" or "Ringo" or "Yoko"))
also evaluates to "Paul"
.
The most straightforward solution is to just list
[n for n in Names if "Beatle" in n and ("Paul" in n or "Ringo" in n or "Yoko" in n)]
But you could use any
to get a bit closer to what you tried initially:
[n for n in Names if "Beatle" in n and any(x in n for x in ("Paul", "Ringo", "Yoko"))]