How to Convert Boolean Values to Integer in Python

Recently, I was working on a Python project where I had to process survey data collected from employees across different states in the USA. The responses included yes/no answers, which were stored as Boolean values (True or False).

The challenge was that I needed to perform calculations, and Booleans don’t work directly in mathematical operations. So, I had to find a way to convert Boolean values to integers (1 for True and 0 for False).

In this tutorial, I’ll walk you through five different methods I’ve personally used in my Python development journey.

Method 1 – Use the int() Constructor

The easiest way to convert a Boolean value into an integer is by using Python’s built-in int() function. This method works instantly because Booleans in Python are actually a subtype of integers.

# Example: Convert Boolean to Integer using int()

# Single values
print(int(True))   # Output: 1
print(int(False))  # Output: 0

# List of Boolean values
bool_list = [True, False, True, True, False]
int_list = [int(value) for value in bool_list]

print(int_list)  # Output: [1, 0, 1, 1, 0]

You can see the output in the screenshot below.

python boolean to integer

I often use this method when I just need a quick conversion without writing extra code. It’s clean, simple, and works for both single values and lists.

Method 2 – Use Conditional Expressions

Sometimes I prefer using a ternary conditional expression. This method is especially useful when I want to make the conversion more explicit.

# Example: Convert Boolean to Integer using conditional expression

def bool_to_int(value):
    return 1 if value else 0

print(bool_to_int(True))   # Output: 1
print(bool_to_int(False))  # Output: 0

You can see the output in the screenshot below.

python bool conversion

I like this approach when I need to make the code more readable for beginners. It clearly shows that True maps to 1 and False maps to 0.

Method 3 – Use Arithmetic Operations

Another interesting trick is to use arithmetic operations. Since True behaves like 1 and False like 0, multiplying or adding them works perfectly.

# Example: Convert Boolean to Integer using arithmetic

is_active = True
is_verified = False

print(is_active * 1)     # Output: 1
print(is_verified + 0)   # Output: 0

You can see the output in the screenshot below.

python boolean to int

I’ve used this method when working with data transformations in financial applications. It’s fast and doesn’t require any additional functions.

Method 4 – Use NumPy Arrays

When working with large datasets, I often use NumPy. NumPy automatically converts Boolean arrays into integer arrays with a simple typecast.

# Example: Convert Boolean array to Integer array using NumPy

import numpy as np

bool_array = np.array([True, False, True, False, True])
int_array = bool_array.astype(int)

print(int_array)  # Output: [1 0 1 0 1]

You can see the output in the screenshot below.

python bool to int

This method is perfect if you’re analyzing survey data, stock market signals, or machine learning features. It’s efficient and optimized for performance.

Method 5 – Use Dictionary Mapping

Sometimes, I like to use a dictionary-based mapping approach. This method is helpful when I want to extend the logic later on.

# Example: Convert Boolean to Integer using dictionary mapping

bool_to_int_map = {True: 1, False: 0}

print(bool_to_int_map[True])   # Output: 1
print(bool_to_int_map[False])  # Output: 0

# For a list of values
bool_list = [True, False, True, False]
int_list = [bool_to_int_map[val] for val in bool_list]

print(int_list)  # Output: [1, 0, 1, 0]

I’ve used this technique in data pipelines where mapping logic often changes. It’s flexible and keeps the code easy to update.

Practical Example – Employee Survey Data

Let me share a real-world example from a project I worked on for a company in California. We had employee survey data where remote work preference was stored as True or False. But the HR department wanted the data in numeric form (1 for Yes, 0 for No).

Here’s how I solved it:

# Example: Convert survey Boolean data to integers

employees = [
    {"name": "Alice", "remote": True},
    {"name": "Bob", "remote": False},
    {"name": "Charlie", "remote": True},
    {"name": "Diana", "remote": False}
]

# Convert using int()
for emp in employees:
    emp["remote"] = int(emp["remote"])

print(employees)

Output:

[
    {'name': 'Alice', 'remote': 1},
    {'name': 'Bob', 'remote': 0},
    {'name': 'Charlie', 'remote': 1},
    {'name': 'Diana', 'remote': 0}
]

This approach made it easy for HR to analyze remote work trends across different departments. It’s a simple example, but it shows how useful this conversion can be in real business scenarios.

Things to Keep in Mind

  • Booleans are already a subtype of integers in Python (True == 1, False == 0).
  • Choose the method based on your use case (small scripts vs. large datasets).
  • NumPy is best for performance-heavy tasks.
  • Dictionary mapping is best for scalable logic.

Conclusion

Converting Boolean values to integers in Python is simple once you know the different methods. I’ve shown you five approaches: int(), conditional expressions, arithmetic, NumPy, and dictionary mapping.

Each method works well, but the best choice depends on your project requirements. I personally use int() for quick conversions and NumPy for large datasets.

You may also read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.