Convert List from 1/0 Dummy Integer to Boolean in Python (2 Examples)

 

This tutorial explains how to convert lists of 1/0 dummy integers to booleans in the Python programming language.

Table of contents:

Let’s dive right in!

 

Introducing Example Data

The following data will be used as the sample data for this Python tutorial.

sl_int=[1, 0, 1, 1, 0, 1]            # create sample data
print(sl_int)                        # print sample data
# [1, 0, 1, 1, 0, 1]

The previous output shows that sl_int is a list of 1/0 integers, i.e. a dummy variable.

Let’s check the data type of our list elements.

for element in sl_int:               # print sample data type
  print(type(element))
# <class 'int'>
# <class 'int'>
# <class 'int'>
# <class 'int'>
# <class 'int'>
# <class 'int'>

You can see that all elements are in the integer class.

Let’s move on to the first example of converting integers in a list to booleans.

 

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

In Example 1, I’ll explain how to change the integer type of list elements to booleans via the list() and map() functions. The converted list is named sl_bool1, see the script below.

sl_boo1=list(map(bool, sl_int))      # apply list() & map() functions
print(sl_boo1)                       # print output of list() & map()
# [True, False, True, True, False, True]

As shown above, the value 1 in our input data was converted to the logical value True, and the value 0 was transformed to False.

Let’s check if the attempt to convert our data to the boolean data type was successful.

for element in sl_boo1:              # print converted data type
  print(type(element))                
# <class 'bool'>
# <class 'bool'>
# <class 'bool'>
# <class 'bool'>
# <class 'bool'>
# <class 'bool'>

As seen, now all list items have the boolean data type in the converted list sl_boo1.

 

Example 2: Transform List of Integers to Boolean Type Using List Comprehension

In this example, I’ll illustrate how to transform integer list elements to booleans utilizing list comprehension.

sl_boo2=[bool(x) for x in sl_int]    # list comprehension method
print(sl_boo2)                       # print output of list comprehension
# [True, False, True, True, False, True]

Let’s check if the code above ran properly.

for element in sl_boo2:              # print converted data type
  print(type(element))             
# <class 'bool'>
# <class 'bool'>
# <class 'bool'>
# <class 'bool'>
# <class 'bool'>
# <class 'bool'>

Looks like so, you can see how all data classes are booleans above.

 

Video & Further Resources

Have a look at the following video which I have published on my YouTube channel. In the video, I’m showing the Python programming codes of this article.

 

The YouTube video will be added soon.

 

In addition, you might want to read the other tutorials on my homepage. A selection of other tutorials is shown below.

 

In this article, I have illustrated how to transform integer elements to booleans in a list in the Python programming language. In case you have additional questions, please let me know in the comments section. Furthermore, please subscribe to my email newsletter to receive updates on new tutorials.

 

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