Convert List from Boolean to 1/0 Dummy Integer in Python (2 Examples)
This tutorial illustrates how to convert lists of booleans to integers in Python.
The content of the tutorial looks as follows:
So now is the part you have been waiting for: the examples!
Example Data
The first step is to construct data that we can use in the following examples. For demonstration, I created a list containing boolean elements and named it sl_boo.
sl_boo=[True, True, True, False, False, True] # create sample data print(sl_boo) # print sample data # [True, True, True, False, False, True] |
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 console shows the structure of the list sl_boo. As seen, there are 4 True and 2 False values contained. To check if the data class of items is boolean, the type() and print() functions are called 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'> |
for element in sl_boo: # print sample data type print(type(element)) # <class 'bool'> # <class 'bool'> # <class 'bool'> # <class 'bool'> # <class 'bool'> # <class 'bool'>
You see how all elements of sl_bool are in the boolean class. Let’s move on to the first example, which shows one way of converting list elements from boolean to 1/0 dummy integer values.
Example 1: Transform List of Booleans to Integers Using list() & map() Functions
In this example, I’ll demonstrate how to transform a list of booleans into integers using the list() and map() functions. For the execution, see below.
sl_int1=list(map(int, sl_boo)) # apply list() & map() functions print(sl_int1) # print output of list() & map() # [1, 1, 1, 0, 0, 1] |
sl_int1=list(map(int, sl_boo)) # apply list() & map() functions print(sl_int1) # print output of list() & map() # [1, 1, 1, 0, 0, 1]
You see how the True and False values are converted in the sl_int1 list into 1s and 0s, respectively. Let’s check now if the list items are indeed integers.
for element in sl_int1: # print converted data type print(type(element)) # <class 'int'> # <class 'int'> # <class 'int'> # <class 'int'> # <class 'int'> # <class 'int'> |
for element in sl_int1: # print converted data type print(type(element)) # <class 'int'> # <class 'int'> # <class 'int'> # <class 'int'> # <class 'int'> # <class 'int'>
The output above shows that each item is an integer in our new list sl_int1.
Example 2: Transform List of Booleans to Integers Using List Comprehension
In Example 2, I’ll show how to change the boolean data type to integer in a Python list using list comprehension. See how it is implemented for the new list called sl_int2.
sl_int2=[int(x) for x in sl_boo] # list comprehension method print(sl_int2) # print output of list comprehension # [1, 1, 1, 0, 0, 1] |
sl_int2=[int(x) for x in sl_boo] # list comprehension method print(sl_int2) # print output of list comprehension # [1, 1, 1, 0, 0, 1]
Like in Example 1, the True values were replaced with 1s and the False values were replaced with 0s, resulting in a 1/0 dummy variable. You know what is next: checking the data type of the list elements!
for element in sl_int2: # print converted data type print(type(element)) # <class 'int'> # <class 'int'> # <class 'int'> # <class 'int'> # <class 'int'> # <class 'int'> |
for element in sl_int2: # print converted data type print(type(element)) # <class 'int'> # <class 'int'> # <class 'int'> # <class 'int'> # <class 'int'> # <class 'int'>
As seen above, the types of all items are integers. Looks like we did a good job!
Video & Further Resources
In case you need further explanations on the Python programming syntax of this article, I can recommend watching the following video on my YouTube channel. In the video, I show the Python codes of this tutorial.
The YouTube video will be added soon.
Furthermore, you might read the related tutorials on this homepage:
- Convert 1/0 Integer Dummy to True/False Boolean in Columns of pandas DataFrame in Python
- Convert True/False Boolean to 1/0 Dummy Integer in pandas DataFrame Column in Python
- Introduction to Python Programming
This tutorial has illustrated how to change the boolean data class to integer in a Python list in the Python programming language. Don’t hesitate to let me know in the comments section below, in case you have any further questions. Furthermore, don’t forget to subscribe to my email newsletter for updates on new tutorials.
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.
Statistics Globe Newsletter