Flip the boolean values in a list in Python

avatar

Borislav Hadzhiev

Last updated: Aug 28, 2022

banner

Photo from Unsplash

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
import numpy as np # ✅ Flip the boolean values in a list my_list = [True, True, False, False] new_list = [not item for item in my_list] print(new_list) # 👉️ [False, False, True, True] # ----------------------------------------------- # ✅ Flip a numpy array of booleans arr = np.array([True, True, False, False], dtype=bool) result = np.logical_not(arr) print(result) # 👉️ [False False True True]

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 'hello') # 👉️ 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() #

To flip the boolean values in a list:

  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 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 #

Use the numpy.logical_not() method to negate the boolean values in a numpy array, e.g. arr = np.array([True, True, False, False], dtype=bool). The logical_not() method applies the logical not operator to the elements in the array and returns the result.

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]

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.

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.