Print "foo" If An Element Is In A List
Solution 1:
l = [1, 2, 3]
x = 1
if x in l: print "Foo"
I'm not being a smart ass, this is the way to do it in one line. Or, if you're using Python3:
if x in l: print("Foo")
Solution 2:
lambda
creates, well a lambda. It needs to be called to execute it. You cannot do this that way, because Python doesn't allow statements in this context, only expressions (including function calls).
To make print
a function in Python 2.x, try:
from __future__ import print_function
x in l andprint('foo')
Be wary though. If you try:
x in l and print('foo') or print('bar')
it won't work, because print returns None
, so the first and
expression is False, so both print
s will be executed. In Python 3.x you don't need the import.
If you won't have complex short-circuiting (i.e. just one and
or or
), or you know your functions or expressions won't surprise the short-circuiting logic, there's nothing wrong with the code. Otherwise, try the non-short-circuiting 1-liner:
print('foo') if x in l elseprint('bar')
This form is recommended only if the probability/expectation of the conditional to be True is vastly higher than being False. Otherwise, plain good-old if-else
is the way to go.
Solution 3:
Getting rid of the shortcomings of print as a statement in Python2.x using from __future__ import print_function
is the first step. Then the following all work:
x in l and (lambda: print("yes"))() # what an overkill!
(x in l orprint("no")) andprint("yes") # note the order, print returns Noneprint("yes") if x in l elseprint("no") # typical A if Cond else Yprint("yes"if x in l else"no") # a more condensed form
For even more fun, if you're into this, you can consider this - prints and returns True or False, depending on the x in l
condition (to get the False I used the double not):
defcheck_and_print(x, l):
return x in l andnotprint("yes") ornotnotprint("no")
That was ugly. To make the print transparent, you could define 2 other version of print, which return True or False. This could actually be useful for logging:
deftrueprint(*args, **kwargs):
print(*args, **kwargs)
returnTruedeffalseprint(*args, **kwargs):
returnnot trueprint(*args, **kwargs)
result = x in l and trueprint("yes") or falseprint("no")
Solution 4:
If you want to print something different in both true and false cases, use a conditional expression to create the value to print:
print ('foo' if x in l else 'bar')
.If you just want a function in Python 2 that outputs, you can try
sys.stdout.write
(after you firstimport sys
of course), but keep in mind that this is nowhere near as flexible; here you're treating the standard output as a file-like object (which it is).lambda
almost certainly buys you nothing here.Using and-or chaining tricks is incredibly un-Pythonic. The fact that people struggled with these hacks anyway, knowing how awful they were, was exactly why those conditional expressions from point 1 were added to the language. There was a lot of discussion regarding syntax.
Post a Comment for "Print "foo" If An Element Is In A List"