Better way to negate a boolean list?

Stargaming stargaming at gmail.com
Sun Feb 10 11:27:00 EST 2008


On Sun, 10 Feb 2008 08:46:24 +0100, David Trémouilles wrote:
[snip]
> I tried:
>  >>> map(not, boolean_list)
> but it seems that "not" is not a function.

`not` is not a function, indeed. It is a keyword, allowing you to write 
``not x`` instead of ``not(x)``. 

You can of course write a function that just returns its input negated 
and pass this function to `map`. Since Python comes with batteries 
included, there is such a function already in the `operator module 
<http://docs.python.org/lib/module-operator.html>`_::

    import operator
    map(operator.not_, boolean_list)
    #               ^ underscore to distinguish from the keyword

Cheers,



More information about the Python-list mailing list