In Python, when dealing with data, it is common to work with different types and formats. One common task is converting string representations of truth values into their corresponding boolean values. Given a string list, convert the string truth values to Boolean values using Python.
Input : test_list = ["True", "False", "True", "False"]
Output : [True, False, True, False]
Explanation : In This, we are converting String booleans to actual Boolean in Python.
Ways to Convert Python String to Boolean
In Python, there are many different approaches to convert Python String to Boolean. Here are some methods given below:
- Using Bool()
- Using Eval()
- Using List Comprehension
- Using Map()
- Using ast.literal_eval()
- Using Dictionary
Convert String to Bool in Python using Bool()
In this method, we are converting Python string to boolean using bool(). The bool() method in general takes only one parameter(here x), on which the standard truth testing procedure can be applied. If no parameter is passed, then by default it returns False.
Python3
string = "Lazyroar" bool_value = bool (string) print (bool_value) |
Output
True
Time Complexity: O(1)
Auxiliary Space: O(1)
Convert String to Bool in Python using Eval()
This method converts a Python string to a boolean using eval(). Python eval() function parse the expression argument and evaluate it as a Python expression and runs Python expression(code) within the program.
Python3
eval ( "False" ) |
Output
False
Time Complexity: O(1)
Auxiliary Space: O(1)
Convert String to Bool in Python using List Comprehension
This method converts a Python string to a boolean using List comprehension. In this, we check for the true value, and the rest of the values are automatically converted to a False boolean.
Python3
# initializing lists test_list = [ "True" , "False" , "TRUE" , "FALSE" , "true" , "false" ] # printing string print ( "The original list : " , test_list) # using list comprehension to check "True" string res = [ele.lower().capitalize() = = "True" for ele in test_list] # printing results print ( "The converted Boolean values : " , res) |
Output
The original list : ['True', 'False', 'True', 'True', 'False']
The converted Boolean values : [True, False, True, True, False]
Time Complexity: O(1)
Auxiliary Space: O(1)
Convert String to Bool in Python using Map()
This method converts a Python string to a boolean using Map() and lambda. In this, we apply the same approach, just a different way to solve the problem. The map() extends the logic of values computed by the lambda function.
Python3
# initializing lists test_list = [ "True" , "False" , "TRUE" , "FALSE" , "true" , "false" ] # printing string print ( "The original list : " , test_list) # using map() to extend and lambda to check "True" string res = list ( map ( lambda ele: ele.lower(). capitalize() = = "True" , test_list)) # printing results print ( "The converted Boolean values : " ,res) |
Output
The original list : ['True', 'False', 'True', 'True', 'False']
The converted Boolean values : [True, False, True, True, False]
Time Complexity: O(n)
Space Complexity: O(n)
Convert String to Bool in Python using ast.literal_eval()
This method converts a Python string to a boolean using ast.literal_eval(). The ast.literal_eval() function reliably evaluates Python expressions from strings, including simple literals like strings, numbers, and boolean values.
Python3
# Import the ast module. import ast # Define a function called convert_string_to_bool that takes a string as input. def convert_string_to_bool(str_value): try : # Use the ast.literal_eval function to safely evaluate the input string. value = ast.literal_eval(str_value) except (SyntaxError, ValueError): # If an error occurs during evaluation, return False. return False # Convert the evaluated value to a boolean using the bool function. # Return the boolean value. return bool (value) str_value = 'True' # Call the convert_string_to_bool function with the input string. # Store the resulting boolean value in the bool_value variable. bool_value = convert_string_to_bool(str_value) print (bool_value) |
True
Time complexity: O(1)
Space complexity: O(1).
Convert String to Bool in Python using Dictionary
In this method, we are converting a Python string to a boolean using Dictionary. Using a dictionary involves creating a mapping between string representations and their corresponding boolean values. This approach allows you to handle custom or non-standard string truth values and convert them into boolean values.
Python3
string_list = [ "True" , "False" , "True" , "False" ] dict1 = { "true" : True , "false" : False } bool_list = [dict1.get(item.lower(), False ) for item in string_list] print (bool_list) |
[True, False, True, False]
Time complexity: O(n)
Space complexity: O(n)