Convert List from Character String to Boolean in Python (3 Examples)

 

Hi! This tutorial will show you 3 ways to convert a list of character strings to booleans in the Python programming language.

First, though, here is an overview of this tutorial:

Let’s dive into Python code!

 

Create List of Character Strings

To create the list of character strings that we will use in this tutorial, please run the line of code below in your preferred Python IDE:

string_list = ["True", "False", "True", "False"]

We have our sample list of strings called string_list, let’s convert it!

 

Example 1: Transform List of Character Strings to Booleans via List Comprehension

In this first example, we will use list comprehension to convert the list of character strings to boolean values:

to_bool = [item.lower().capitalize() == "True" for item in string_list]
print(to_bool)
 
# [True, False, True, False]

We searched for only the “True” value in the above code, while the program automatically returned the rest as “False”.

 

Example 2: Transform List of Character Strings to Booleans via eval() Function & for Loop

In this second example, we will run a for loop through the eval() function to convert the list of character strings to boolean values:

to_bool = []
for i in string_list:
  to_bool.append(eval(i))
 
print(to_bool)
 
# [True, False, True, False]

The eval() function evaluates each item in the list as a Python expression.
 

Example 3: Transform List of Character Strings to Booleans via map() & lambda Functions

In this third and final example, we will use a combination of the map() function and the lambda function to iterate through the list of character strings and convert it to boolean values:

to_bool = list(map(lambda item: item.lower().capitalize() == "True", string_list))
print(to_bool)
 
# [True, False, True, False]

 

So, that is how to convert a list of character strings to booleans in Python.

I hope you have found this tutorial helpful!

Video, Further Resources & Summary

Do you need more explanations on how to convert a list of character strings 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 character strings to booleans in Python.

 

The YouTube video will be added soon.

 

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

This post has shown how to convert a list of character strings 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