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

 

In this Python tutorial you’ll learn how to transform lists of booleans to floats .

Table of contents:

Let’s get started!

 

Example Data

We’ll use the following data as a basis for this Python tutorial. It is a list named sl_boo containing some boolean elements.

sl_boo=[True, True, True, False, False, True]    # create sample data
print(sl_boo)                                    # print sample data
 
# [True, True, True, False, False, True]

The previous Python code prints the items of sl_boo. As seen, there are 4 True and 2 False values. To confirm the data class of the items, the type() and print() functions are employed within a for loop as follows.

for element in sl_boo:                           # print sample data type
  print(type(element)) 
 
# <class 'bool'>
# <class 'bool'>
# <class 'bool'>
# <class 'bool'>
# <class 'bool'>
# <class 'bool'>

The output above confirms that the elements in sl_boo have the boolean data type.

 

Example 1: Transform List of Booleans to Floats Using list() & map() Functions

In this example, I’ll demonstrate how to transform a list of booleans into floats using the built-in list() and map() functions. The transformed data is named sl_flt1, check below.

sl_flt1=list(map(float, sl_boo))                 # apply list() & map() functions
print(sl_flt1)                                   # print output of list() & map()
 
# [1.0, 1.0, 1.0, 0.0, 0.0, 1.0]

You see how the elements of sl_flt1 are now decimals (i.e. 1.0 and 0.0).

By the way, you may also convert a boolean list to a 1/0 dummy variable that contains integers instead of decimals. Have a look here for more details.

However, let’s now double-check the data type of the items of sl_flt1!

for element in sl_flt1:                          # print converted data type 
  print(type(element))                           
 
# <class 'float'>
# <class 'float'>
# <class 'float'>
# <class 'float'>
# <class 'float'>
# <class 'float'>

Good job! All items are floats in the transformed list sl_flt1.

 

Example 2: Transform List of Booleans to Floats Using List Comprehension

In Example 2, I’ll show how to change the boolean data type to float in a Python list using list comprehension. See how it is created under the new list named sl_flt2.

sl_flt2=[float(x) for x in sl_boo]               # list comprehension method
print(sl_flt2)                                   # print output of list comprehension
 
# [1.0, 1.0, 1.0, 0.0, 0.0, 1.0]

Like in Example 1, the True values were replaced with 1.0s and the False values were replaced with 0.0s. You know what is next: checking the data type of the new list elements!

for element in sl_flt2:                           # print converted data type
  print(type(element))                            
 
# <class 'float'>
# <class 'float'>
# <class 'float'>
# <class 'float'>
# <class 'float'>
# <class 'float'>

Well done! Now we have a list of floats of 0.0s and 1.0s as desired.

 

Video, Further Resources & Summary

If you need further explanations on the Python codes of this article, I recommend having a look at the following video on my YouTube channel. I’m explaining the Python code of this tutorial in the video.

 

The YouTube video will be added soon.

 

In addition, you could read the related posts which I have published on this website:

 

Summary: This article has illustrated how to replace a list of booleans with floats in the Python programming language. In case you have further comments and/or questions, please let me know in the comments below.

 

Cansu Kebabci R Programmer & Data Scientist

This page was created in collaboration with Cansu Kebabci. Have a look at Cansu’s author page to get more information about her professional background, a list of all his tutorials, as well as an overview on her other tasks on Statistics Globe.

 

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