Convert List from Float to Boolean in Python (4 Examples)

 

Hi! This tutorial will show you 4 simple ways to convert a list of floats to booleans in the Python programming language.

First, though, here is a quick overview of this tutorial:

Let’s get into the Python code!

 

Create List of Floats

Here, we will create the sample list of floats that we will convert to booleans in this tutorial. Therefore, in your preferred Python IDE, run the line of code below to create the list of floats:

float_list = [1.2, 0.0, 3.1, 4.5]

See, float_list contains 4 floating point numbers.

 

Example 1: Convert List from Float to Boolean Using List Comprehension

In this first example, we will use list comprehension to convert the list of floats to booleans.

bool_list = [True if x > 0 else False for x in float_list]
print(bool_list)
 
# [True, False, True, True]

The logic is simply that if an item in the list is greater than 0.0, it is indeed a float, and the program will return True for that item. But if the item in the list is not greater than 0.0, it is not a float, and the program would return False.

 

Example 2: Convert List from Float to Boolean Using For Loop

In this second example, we will write a for loop that will loop through each item in the list of floats and determine whether it is a float or not, using the “if” and “else” statement.

bool_list = []
for i in float_list:
  if i > 0:
    bool_list.append(True)
  else:
    bool_list.append(False)
 
print(bool_list)
 
 
# [True, False, True, True]

The program works exactly like the list comprehension method demonstrated above.

 

Example 3: Convert List from Float to Boolean Using map() Function

In this third example, we will use the map() function to iterate through the list of floats and convert it to booleans and the list() function to list the converted elements.

bool_list = list(map(bool, float_list))
print(bool_list)
 
# [True, False, True, True]

As seen, map() and list() transform the floats to booleans successfully as the previous alternatives.

 

Example 4: Convert List from Float to Boolean Using NumPy Array

For this example, we will need to install and import Python’s NumPy library, which is used for scientific computing in Python.

 
# pip install numpy    # command to install NumPy in prompt
 
# import numpy
import numpy as np

Next, we will make use of the np.array() function to convert the list of floats to booleans, like so:

bool_list = np.array(float_list) > 0
bool_list = bool_list.tolist()
print(bool_list)
 
 
# [True, False, True, True]

The np.array(float_list) > 0 returns a NumPy array of booleans, while the tolist() method converts it into a list object.

So, there you have it! We have demonstrated 4 simple ways to convert a list of floats to booleans in Python. I hope you found this tutorial helpful!
 

Video, Further Resources & Summary

Do you need more explanations on how to convert a list of floats to booleans in Python? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.

In the video, we explain in some more detail how to convert a list of floats to booleans in Python.

 

The YouTube video will be added soon.

 

Furthermore, I encourage you to check out other interesting Python list tutorials on Statistics Globe, starting with these ones:

This post has shown how to convert a list of floats to booleans in Python. In case you have further questions, you may leave a comment below.

 

R & Python Expert Ifeanyi Idiaye

This page was created in collaboration with Ifeanyi Idiaye. You might check out Ifeanyi’s personal author page to read more about his academic background and the other articles he has written for the Statistics Globe website.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Top