find non-empty value in a dictionary

Alex Martelli aleax at aleax.it
Wed Apr 16 05:13:40 EDT 2003


Dylan Reinhardt wrote:

> What about:
> 
> if x:
>    <stuff>
> elif y:
>    <stuff>
> 
> Etc, etc.
> 
> Seems to work fine with a float value of 0.  There may be some edge case
> where this doesn't work, but if there is, I haven't found it.
> 
> If you're just looking for the result (not which variable had the
> non-zero value), you could do something like (untested):
> 
> result = max([d[var] / d['P'] for var in ['X','Y','Z']])

This will fail if the non-zero entry is < 0 ...


Alex





More information about the Python-list mailing list