Python | Ways to convert Boolean values to integer
Last Updated :
12 Jul, 2025
Given a boolean value(s), write a Python program to convert them into an integer value or list respectively. Given below are a few methods to solve the above task.
Convert Boolean values to integers using int()
Converting bool to an integer using Python typecasting.
Python3
# Initialising Values
bool_val = True
# Printing initial Values
print("Initial value", bool_val)
# Converting boolean to integer
bool_val = int(bool_val == True)
# Printing result
print("Resultant value", bool_val)
Output:
Initial value True
Resultant value 1
Time Complexity: O(1)
Auxiliary Space: O(1)
Convert Boolean values to integers using the Naive Approach
Converting bool to an integer using Python loop.
Python3
# Initialising Values
bool_val = True
# Printing initial Values
print("Initial value", bool_val)
# Converting boolean to integer
if bool_val:
bool_val = 1
else:
bool_val = 0
# Printing result
print("Resultant value", bool_val)
Output:
Initial value True
Resultant value 1
Convert Boolean values to integers using NumPy
In the case where a boolean list is present.
Python3
import numpy
# Initialising Values
bool_val = numpy.array([True, False])
# Printing initial Values
print("Initial values", bool_val)
# Converting boolean to integer
bool_val = numpy.multiply(bool_val, 1)
# Printing result
print("Resultant values", str(bool_val))
Output:
Initial values [ True False]
Resultant values [1 0]
Convert Boolean values to integers using map()
In a case where a boolean list is present.
Python3
# Initialising Values
bool_val = [True, False]
# Printing initial Values
print("Initial value", bool_val)
# Converting boolean to integer
bool_val = list(map(int, bool_val))
# Printing result
print("Resultant value", str(bool_val))
Output:
Initial value [True, False]
Resultant value [1, 0]
Using List comprehension
This approach uses list comprehension to iterate through the list 'bool_val' and applies the int() function to each element, which converts the Boolean value to its integer equivalent (1 for True and 0 for False).
Python3
# Initialising Values
bool_val = [True, False]
# Printing initial Values
print("Initial values", bool_val)
# Converting boolean to integer using list comprehension
bool_val = [int(b) for b in bool_val]
# Printing result
print("Resultant values", str(bool_val))
#This code is contributed by Edula Vinay Kumar Reddy
OutputInitial values [True, False]
Resultant values [1, 0]
Time complexity: O(n)
Space complexity: O(n)
Using the ternary operator to convert boolean to integer:
Approach:
Create a boolean variable b with value True
Use the ternary operator to check if b is True. If it is, assign 1 to the integer variable i, otherwise assign 0.
Print the value of i.
Python3
b = True
i = 1 if b else 0
print(i)
Time Complexity: O(1)
Auxiliary Space: O(1)
Similar Reads
Convert Python boolean values to strings Yes or No When you're using true or false values in Python, changing them to "Yes" or "No" strings can make your code easier to read. This article shows different ways to do it, giving you choices based on how you like to write your code. Here, we will convert the Python Boolean values to string as Yes or No
3 min read
Python - Convert Binary tuple to Integer Given Binary Tuple representing binary representation of a number, convert to integer. Input : test_tup = (1, 1, 0) Output : 6 Explanation : 4 + 2 = 6. Input : test_tup = (1, 1, 1) Output : 7 Explanation : 4 + 2 + 1 = 7. Method #1 : Using join() + list comprehension + int() In this, we concatenate t
5 min read
Python - Convert String Truth values to Boolean Converting string truth values like "True" and "False" into their corresponding boolean values in Python is a straightforward yet useful operation. Methods such as dictionaries, direct comparison, and built-in functions offer simple and efficient ways to achieve this transformation for various use c
2 min read
Ways to concatenate boolean to string - Python Concatenating a boolean to a string in Python involves converting the boolean value (True or False) into a string format and then combining it with other string elements. For example, given s = "Facts are" and v = True, the expected output after concatenation is "Facts are True". Let's explore diffe
2 min read
Ways to concatenate boolean to string - Python Concatenating a boolean to a string in Python involves converting the boolean value (True or False) into a string format and then combining it with other string elements. For example, given s = "Facts are" and v = True, the expected output after concatenation is "Facts are True". Let's explore diffe
2 min read
Python | Count true booleans in a list Given a list of booleans, write a Python program to find the count of true booleans in the given list. Examples: Input : [True, False, True, True, False] Output : 3 Input : [False, True, False, True] Output : 2 Method #1: Using List comprehension One simple method to count True booleans in a list i
3 min read