Negate a boolean or a List of Booleans in Python

avatar
Borislav Hadzhiev

Last updated: Apr 9, 2024
4 min

banner

# Table of Contents

  1. Negate a boolean in Python
  2. Flip the boolean values in a List in Python
  3. Negate the boolean values in a NumPy array

# Negate a boolean in Python

Use the not operator to negate a boolean, e.g. result = not my_bool.

The not operator will negate the boolean value, returning True if the value is False and False if the value is True.

main.py
my_bool = True result = not my_bool print(result) # ๐Ÿ‘‰๏ธ False

negate boolean

The code for this article is available on GitHub

We used the not operator to negate a boolean value.

main.py
print(not True) # ๐Ÿ‘‰๏ธ False print(not False) # ๐Ÿ‘‰๏ธ True

The not operator returns True if the value is falsy and False if the value is truthy.

main.py
print(not True) # ๐Ÿ‘‰๏ธ False print(not False) # ๐Ÿ‘‰๏ธ True print(not 'bobbyhadz.com') # ๐Ÿ‘‰๏ธ False print(not '') # ๐Ÿ‘‰๏ธ True

using not operator

All values that are not truthy are considered falsy. The falsy values in Python are:

  • constants defined to be falsy: None and False.
  • 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).

# Negate a boolean using operator.not_

You can also use the operator.not_ method to negate a boolean.

main.py
import operator print(operator.not_(True)) # ๐Ÿ‘‰๏ธ False print(operator.not_(False)) # ๐Ÿ‘‰๏ธ True

negate boolean using operator not

The code for this article is available on GitHub

The operator.not_ method negates the given object and returns the result.

The operator.not_ method is mostly used when you need to flip a list of boolean values with map().

main.py
import operator list_of_booleans = [True, True, False, False] new_list = list(map(operator.not_, list_of_booleans)) print(new_list) # ๐Ÿ‘‰๏ธ [False, False, True, True]

# Don't use the bitwise NOT operator to negate booleans

Note that you shouldn't use the bitwise ~ not operator to negate boolean values.

main.py
print(~True) # ๐Ÿ‘‰๏ธ -2 print(~False) # ๐Ÿ‘‰๏ธ -1

The bitwise ~ not operator returns unexpected results when used with boolean values because booleans are a subclass of int.

main.py
print(isinstance(True, int)) # ๐Ÿ‘‰๏ธ True

The operator doesn't return the negated boolean value, it returns the negated integer value.

# Flip the boolean values in a list in Python

To flip the boolean values in a list:

  1. Use a list comprehension to iterate over the list.
  2. Use the not operator to flip each boolean value.
  3. The new list will contain the negation of the boolean values in the original list.
main.py
my_list = [True, True, False, False] new_list = [not item for item in my_list] print(new_list) # ๐Ÿ‘‰๏ธ [False, False, True, True]

flip boolean values in a list

The code for this article is available on GitHub

We used a list comprehension to flip the boolean values in a list.

List comprehensions are used to perform some operation for every element or select a subset of elements that meet a condition.

On each iteration, we use the not operator to flip the current boolean value and return the result.

main.py
my_list = [True, True, False, False] new_list = [not item for item in my_list] print(new_list) # ๐Ÿ‘‰๏ธ [False, False, True, True]

The not operator returns True if the value is falsy and False if the value is truthy.

main.py
print(not True) # ๐Ÿ‘‰๏ธ False print(not False) # ๐Ÿ‘‰๏ธ True print(not 'bobbyhadz.com') # ๐Ÿ‘‰๏ธ False print(not '') # ๐Ÿ‘‰๏ธ True

All values that are not truthy are considered falsy. The falsy values in Python are:

  • constants defined to be falsy: None and False.
  • 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).

Alternatively, you can use the map() function.

# Flip the boolean values in a list using map()

This is a three-step process:

  1. Pass a lambda function and the list to the map() function.
  2. The map() function will call the lambda function with each boolean in the list.
  3. Use the list() class to convert the map object to a list.
main.py
my_list = [True, True, False, False] new_list = list(map(lambda x: not x, my_list)) print(new_list) # ๐Ÿ‘‰๏ธ [False, False, True, True]
The code for this article is available on GitHub

The map() function takes a function and an iterable as arguments and calls the function with each item of the iterable.

The lambda function gets called with each boolean value in the list and returns the negation of the value.

Lastly, we used the list() class to convert the map object to a list.

The list class takes an iterable and returns a list object.

# Negate the boolean values in a NumPy array

If you need to negate the boolean values in a NumPy Array, use the numpy.logical_not() method.

main.py
import numpy as np arr = np.array([True, True, False, False], dtype=bool) result = np.logical_not(arr) print(result) # ๐Ÿ‘‰๏ธ [False False True True]

Make sure you have NumPy installed to be able to run the code sample.

shell
pip install numpy # ๐Ÿ‘‡๏ธ or with pip3 pip3 install numpy

We used the numpy.logical_not method to negate a NumPy array.

The logical_not method applies the not operator to the elements of the array.

The method returns a new array containing the negation of each element in the original array.

# Negate the boolean values in a NumPy array using bitwise NOT

The bitwise not ~ operator can be used to negate a NumPy array of booleans.

main.py
import numpy as np arr = np.array([True, True, False, False], dtype=bool) result = ~arr print(result) # ๐Ÿ‘‰๏ธ [False False True True]
The code for this article is available on GitHub

You can also use the invert method to achieve the same result.

main.py
import numpy as np arr = np.array([True, True, False, False], dtype=bool) result = np.invert(arr) print(result) # ๐Ÿ‘‰๏ธ [False False True True]

The numpy.invert method computes bit-wise inversion, or bit-wise NOT, element-wise.

The method takes an array of booleans and inverts the values in the array.

There is also a bitwise_not method in NumPy.

main.py
import numpy as np arr = np.array([True, True, False, False], dtype=bool) result = np.bitwise_not(arr) print(result) # ๐Ÿ‘‰๏ธ [False False True True]

The bitwise_not method is an alias for invert.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev