0


For example, we have an if-else statement which could be reduced to a single line:

condition = True
# condition = False

if condition:
    foo = 'Whatever'
else:
    foo = 'Nevermind'

The default reduce-technique is to do that:

foo = 'Whatever' if condition else 'Nevermind'

But also we can do it this way:

foo = {True:'Whatever', False:'Nevermind'}[condition]

Is that good/bad? Why?
Is there any other way to shrink if-else statement?

IMHO, in the dict-case readability is pretty cool since we can see all possible condition values in a well-structured form.

0 Answers0