by zimzam · 9y ago · 0 comments
foo = [transform(bar) for bar in baz.list()]
Adding an if condition looks like this: foo = [transform(bar) for bar in baz.list() if condition(bar)]
But an if/else expression looks like this: foo = [if condition(bar) transform(bar) else other_transform(bar) for bar in baz.list()]
Adding an else requires completely rewriting the line! Super ugly and inconsistent.Edit: formatting... don't know why my the 'list()' in my last example is disappearing
foo = [
if condition(bar) transform(bar) else other_transform(bar) # 1: storage
for bar in base.list() # 2: iteration
if bar_tester(bar). # 3: filtering
]
to each his own I suppose, until the PEP8 police come and get us all!!!