Search code examples
pythonpython-3.xboolean-expressionunary-operator

are there any simple negation boolean operator in python?


i want to make the boolean list to negated boolean list for example, below code has same meaning

lst= [True, False]
neg_lst = list(map(lambda x: not x, lst))

I think that there are the simplest way to make boolean list to be negated. like, unary operator. maybe.

always thanks for a lot of your help.


Solution

  • You can use the not_ function from the operator package:

    from operator import not_
    
    map(not_, some_list)

    Or you can use list comprehension:

    [not x for x in some_list]