You are testing if i is not None or i.isupper()
. This doesn't work when i
is None
:
(i is not None) or (i.isupper())
is evaluated lazily; first the left is evaluated, and only if False
is the second argument evaluated. The first is only False
if i
is None
, so the right-hand expression is only ever evaluated as None.isupper()
.
Use and
instead, and negate the isupper()
test:
x = i if i is not None and not i.isupper() else None
The above expression is the proper logical inversion of i is None or i.isupper()
.
Alternatively use not (..)
around your original expression:
x = i if not (i is None or i.isupper()) else None
Because you are using a regular for
loop, there is no need to use a conditional expression here; the following, assigning to the existing loop variable i
, suffices:
for i in [None,'foo',None,'FOO',None,'bar']:
if i and i.isupper():
i = None
print i