How to Convert Booleans to Integers in Python and viceversa
In Python, booleans (True
and False
) are a subtype of integers.
This guide explains how to convert between booleans and integers, and how to apply these conversions to lists and NumPy arrays.
Boolean to Integer Conversion
Python treats True
as 1
and False
as 0
in numerical contexts. You can use the int()
constructor explicitly, but often it's not even necessary:
bool_t = True
int_1 = int(bool_t) # Explicit conversion
print(int_1) # Output: 1
bool_f = False
int_0 = int(bool_f) # Explicit conversion
print(int_0) # Output: 0
# Often, you can use booleans directly in arithmetic:
print(True + 5) # Output: 6 (True is treated as 1)
print(False * 10) # Output: 0 (False is treated as 0)
int(True)
returns1
.int(False)
returns0
.- This behavior is useful in many scenarios and is the recommended approach when working with numerical boolean values.
Integer to Boolean Conversion
Use the bool()
constructor to convert integers to booleans:
result = bool(1)
print(result) # Output: True
result = bool(0)
print(result) # Output: False
result = bool(-5) # Any non-zero integer is True
print(result) # Output: True
bool(0)
returnsFalse
.bool(any other integer)
returnsTrue
.
Converting Lists of Booleans to Integers
Using a List Comprehension (Recommended)
List comprehensions provide a concise and efficient way to convert a list of booleans:
list_of_booleans = [True, False, False, True]
list_of_integers = [int(item) for item in list_of_booleans]
print(list_of_integers) # Output: [1, 0, 0, 1]
Using map()
You can use map()
, but it's generally less readable than a list comprehension for this simple case:
list_of_booleans = [True, False, False, True]
list_of_integers = list(map(int, list_of_booleans))
print(list_of_integers) # Output: [1, 0, 0, 1]
- The
map
function takes the function and an iterable. - It applies the function to each element of the iterable.
Converting Lists of Integers to Booleans
Using a List Comprehension (Recommended)
list_of_integers = [1, 0, 1, 0, 1, 1]
list_of_booleans = [bool(item) for item in list_of_integers]
print(list_of_booleans) # Output: [True, False, True, False, True, True]
Using map()
list_of_integers = [1, 0, 1, 0, 1, 1]
list_of_booleans = list(map(bool, list_of_integers))
print(list_of_booleans) # Output: [True, False, True, False, True, True]
Converting NumPy Arrays
If you're working with NumPy arrays, you can use the .astype()
method for efficient conversions:
import numpy as np
arr = np.array([True, False, False, True], dtype=bool)
int_arr = arr.astype(int) # Convert boolean array to integer array
print(int_arr) # Output: [1 0 0 1]
# And vice versa
arr = np.array([1,0,1,1], dtype=int)
bool_arr = arr.astype(bool) # Convert an integer array to a boolean array.
print(bool_arr) # Output: [ True False True True]
arr.astype(int)
: Converts a boolean NumPy array to an integer array (True
becomes 1,False
becomes 0). This is very efficient.arr.astype(bool)
: Converts an integer NumPy array to an boolean array (0
becomesFalse
, non-zero becomesTrue
).
6. Converting 'true'/'false' Strings to Integers If you need to convert a string to an integer based on whether it is equal to 'true' or 'false':
str_t = 'true'
int_1 = int(str_t.lower() == 'true')
print(int_1) # Output: 1
print(type(int_1)) # Output: <class 'int'>
str_f = 'false'
int_0 = int(str_f.lower() == 'true')
print(int_0) # Output: 0
print(type(int_0)) # Output: <class 'int'>
- The str is converted to lower case to ensure that case does not affect the comparison.
- The
==
operator compares the lower case string to'true'
and returns the corresponding boolean value. int()
casts the resulting boolean to1
or0
.