How I make multiline list comprehension in Python? Something like:
[
x for x in a:
if check(x):
....
#something multiline here
....
else:
....
#something multiline here
....
]
Of course I know that I can to something like:
def f(x):
if check(x):
....
else:
....
return x
map(a,f)
But I want it without additional functions.
Is it possible?
My understanding is that you can not do it as you suggested.
It's far more readable when you name the function and use list comprehension or map function compared to what you have suggested
You can't, in the way you're thinking; list comprehensions can only contain expressions, not statements.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With