map float string '0.0' puzzle

Mark Day mday at apple.com
Wed Jan 28 00:31:50 CET 2004

```In article <bv6ovb\$c2q\$1 at boulder.noaa.gov>, j vickroy
<jim.vickroy at noaa.gov> wrote:

> >>> row = ('0.0', '1.0', None)
> >>> map(lambda setting: setting and float(setting) or None, row)
> [None, 1.0, None]
> >>> map(lambda setting: setting and (setting,float(setting)) or
> (setting,None), row)
> [('0.0', 0.0), ('1.0', 1.0), (None, None)]
> >>>
>
> Specifically, why is the return value of the first map operation:
> [None, 1.0, None]

Break it down into something simpler so you can see what's going on.
Let's see what that lambda is evaluating to when setting is '0.0':

>>> '0.0' and float('0.0') or None
>>> print '0.0' and float('0.0') or None
None

That expression evaluates as: ('0.0' and float('0.0')) or None
which means ('0.0' and float('0.0')) must be false.

>>> '0.0' and float('0.0')
0.0

Now you see that the expression reduces to: 0.0 or None

The key is that 0.0 is considered false:
>>> bool(0.0)
False
>>> print 0.0 or None
None

which means the expression reduced to the equivalent of: False or None
and so you get None as the result.

Hope that helps.

-Mark

```