Python – Convert String Truth values to Boolean
Given a String List, convert the String Truth values to Boolean values.
Input : test_list = [“True”, “False”, “True”, “False”]
Output : [True, False, True, False]
Explanation : String booleans converted to actual Boolean.Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course
Input : test_list = [“True”]
Output : [True]
Explanation : String boolean converted to actual Boolean.
Method #1 : Using list comprehension
In this, we check for just the True value, rest values are automatically converted to False boolean.
Python3
# Python3 code to demonstrate working of # Convert String Truth values to Boolean# Using list comprehension # initializing liststest_list = ["True", "False", "True", "True", "False"] # printing stringprint("The original list : " + str(test_list)) # using list comprehension to check "True" stringres = [ele == "True" for ele in test_list] # printing results print("The converted Boolean values : " + str(res)) |
The original list : ['True', 'False', 'True', 'True', 'False'] The converted Boolean values : [True, False, True, True, False]
Method #2 : Using map() + lambda
In this, we apply same approach, just the different way to solve the problem. The map() is used to extend logic of values computed by lambda function.
Python3
# Python3 code to demonstrate working of # Convert String Truth values to Boolean# Using map() + lambda # initializing liststest_list = ["True", "False", "True", "True", "False"] # printing stringprint("The original list : " + str(test_list)) # using map() to extend and lambda to check "True" stringres = list(map(lambda ele: ele == "True", test_list)) # printing results print("The converted Boolean values : " + str(res)) |
The original list : ['True', 'False', 'True', 'True', 'False'] The converted Boolean values : [True, False, True, True, False]


