Convert list of booleans to list of integers in Python

avatar

Borislav Hadzhiev

Last updated: Aug 28, 2022

banner

Photo from Unsplash

Convert list of booleans to list of integers in Python #

To convert a list of booleans to a list of integers:

  1. Use a list comprehension to iterate over the list.
  2. Use the int() class to convert each boolean to an integer.
  3. The new list will only contain integers.
main.py
import numpy as np # ✅ Convert a list of booleans to a list of integers list_of_booleans = [True, False, False, True] list_of_integers = [int(item) for item in list_of_booleans] print(list_of_integers) # 👉️ [1, 0, 0, 1] # ----------------------------------------------------- # ✅ convert an array of booleans to array of integers arr = np.array([True, False, False, True], dtype=bool) int_arr = arr.astype(int) print(int_arr) # 👉️ [1 0 0 1]

We used a list comprehension to convert a list of booleans to a list of integers.

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 pass the current boolean to the int() class to convert it to an integer.

main.py
list_of_booleans = [True, False, False, True] list_of_integers = [int(item) for item in list_of_booleans] print(list_of_integers) # 👉️ [1, 0, 0, 1]

The int class returns an integer object constructed from the provided number or string argument.

The constructor returns 0 if no arguments are given.

The new list contains the integer representation of the booleans in the original list.

Alternatively, you can use the map() function.

Convert list of booleans to list of integers using map() #

To convert a list of booleans to a list of integers:

  1. Pass the int() class and the list to the map() function.
  2. The map() function will call the int() class with each boolean in the list.
  3. Use the list() class to convert the map object to a list.
main.py
list_of_booleans = [True, False, False, True] list_of_integers = list(map(int, list_of_booleans)) print(list_of_integers) # 👉️ [1, 0, 0, 1]

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

The int() class gets passed each boolean in the list and converts each value to an integer.

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.

Convert array of booleans to array of integers using numpy #

Use the astype() method to convert an array of booleans to an array of integers, e.g. int_arr = arr.astype(int). The astype() method will return a copy of the array that contains the integer representation of the values.

main.py
import numpy as np arr = np.array([True, False, False, True], dtype=bool) int_arr = arr.astype(int) print(int_arr) # 👉️ [1 0 0 1]

You can use the astype method on a numpy array to copy the array and cast it to a specified type.

The only parameter we passed to the astype method is the data type to which the array is cast.

To convert the array of booleans to an array of integers, pass the int class to the astype() method.

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.