the PHP ternary operator equivalent on Python

Fredrik Lundh fredrik at
Thu Nov 24 08:41:39 CET 2005

rurpy at wrote:

> I don't find your code any more readable than the OP's
> equivalent code:

the OP's question was

    How you do this in a practic way without
    the use of one-line code ?

> The OPs code make one pass through the dict, your's makes
> two.  I do not know what effect (if any) that has in the case of
> a very large dict.

the OPs code contains five reads and three writes, my and
steven's versions contain four reads and three writes.  the
yet-to-be-released ternary operator would remove one read
from the OPs code, which makes both alternatives equivalent.

dictionary accesses don't involve passes over the dictionary.

there's a comment on readability and code understanding waiting
to be made here, but I'll leave that for another day.


More information about the Python-list mailing list