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

 

In this Python tutorial you’ll learn how to replace values in a boolean list with strings.

The article is structured as follows:

You’re here for the answer, so let’s get straight to the examples!

 

Sample Data

The data below will be used as a basis for this Python tutorial. It is a list named sl_boo and contains some boolean elements. As seen below, there are 4 True and 2 False values in the list.

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

Let’s confirm the data class of the items of sl_boo by applying the built-in type() and print() functions of Python in a for loop.

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 previous output reveals that all elements in sl_boo are in the boolean data type.

 

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

The following Python programming code demonstrates how to transform a list of booleans into strings using the built-in list() and map() functions. See how the new list with strings is created under the list name sl_str1 below.

sl_str1=list(map(str, sl_boo))                   # apply list() & map() functions
print(sl_str1)                                   # print output of list() & map()
 
# ['True', 'True', 'True', 'False', 'False', 'True']

Please be aware that the True and False values are in quotation marks in sl_str1, which is a typical feature of character strings in Python.

for element in sl_str1:                          # print converted data type
  print(type(element))  
 
# <class 'str'>
# <class 'str'>
# <class 'str'>
# <class 'str'>
# <class 'str'>
# <class 'str'>

Good job! All items have the string data type in the transformed list sl_str1.

 

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

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

sl_str2=[str(x) for x in sl_boo]                 # list comprehension method
print(sl_str2)                                   # print output of list comprehension
 
# ['True', 'True', 'True', 'False', 'False', 'True']

Like in Example 1, the True and False values are written in quotation marks. What about confirming the data types of the list elements via the type() and print() functions? I can hear you say yes, then check below!

for element in sl_str2:                          # print converted data type
  print(type(element)) 
 
# <class 'str'>
# <class 'str'>
# <class 'str'>
# <class 'str'>
# <class 'str'>
# <class 'str'>

Well done! Now we have a list of character strings of True and False as desired.

 

Video, Further Resources & Summary

Do you want to learn more about the replacement of lists of booleans with strings? Then I recommend watching the following video which I have published on my YouTube channel. In the video, I show the Python syntax of this tutorial in a programming session in Python:

 

The YouTube video will be added soon.

 

Furthermore, you may want to look at some of the other articles on my homepage. A selection of articles on related topics such as character strings and data conversion can be found below:

 

You have learned in this article how to convert lists of booleans to lists of strings in Python programming. Tell me about it in the comments section, in case you have additional questions or comments.

 

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