How to Negate Booleans in Python
Negating a boolean value (changing True
to False
and vice-versa) is a fundamental operation in Python.
This guide explains how to negate booleans using the not
operator, how to apply negation to lists of booleans, and how to work with boolean NumPy arrays. We'll also touch on why you should avoid the bitwise NOT operator (~
) for boolean negation.
Negating a Single Boolean Value with not
The not
operator is the correct and only way to negate a boolean value in Python:
my_bool = True
result = not my_bool
print(result) # Output: False
print(not True) # Output: False
print(not False) # Output: True
not True
evaluates toFalse
.not False
evaluates toTrue
.- The
not
operator is not only used for boolean values, but it works by checking if a value is falsy.
All values that are not truthy are considered falsy. The falsy values in Python are:
- constants defined to be falsy:
None
andFalse
. 0
(zero) of any numeric type.- empty sequences and collections:
""
(empty string),()
(empty tuple),[]
(empty list),{}
(empty dictionary),set()
(empty set),range(0)
(empty range).
Negating Booleans in a List
To negate all boolean values within a list, use a list comprehension or the map
function.
Negating Booleans Using a List Comprehension (Recommended)
List comprehensions provide a concise and readable way to apply not
to each element:
my_list = [True, True, False, False]
new_list = [not item for item in my_list]
print(new_list) # Output: [False, False, True, True]
[not item for item in my_list]
: This creates a new list where each element is the negation of the corresponding element inmy_list
.
Negating Booleans Using map()
(Less Readable)
You can use the map()
function, but it's generally less readable than a list comprehension for this simple case:
import operator
my_list = [True, True, False, False]
new_list = list(map(operator.not_, my_list))
print(new_list) # Output: [False, False, True, True]
- The
map()
function can be used to apply the not operator to a list of booleans. - The
operator.not_
is the function equivalent of the not keyword, and we can use it as an argument inmap()
. - The
list()
is used to convert the result of map into a list.
Negating Boolean Values in a NumPy Array
If you're working with NumPy arrays, use np.logical_not()
or the bitwise NOT operator (~
):
import numpy as np
arr = np.array([True, True, False, False], dtype=bool)
result = np.logical_not(arr)
print(result) # Output: [False False True True]
# Using the bitwise NOT operator (~):
result2 = ~arr
print(result2) # Output: [False False True True]
result3 = np.invert(arr)
print(result3) # Output: [False False True True]
result4 = np.bitwise_not(arr)
print(result4) # Output: [False False True True]
np.logical_not(arr)
: This is the most explicit and readable way to negate a boolean NumPy array.~arr
: The bitwise NOT operator (~
) works correctly on boolean NumPy arrays in this specific case. However, it's generally clearer to usenp.logical_not()
.np.invert()
can also be used.np.bitwise_not()
can also be used.
Avoid the Bitwise NOT Operator (~
) for Booleans
Avoid using the bitwise operator on boolean values, as the bool
class is a subclass of int
.