Tuesday, November 19, 2024
Google search engine
HomeLanguagesPython | Convert heterogeneous type String to List

Python | Convert heterogeneous type String to List

Sometimes, while working with data, we can have a problem in which we need to convert data in string into a list, and the string contains elements from different data types like boolean. This problem can occur in domains in which a lot of data types are used. Let’s discuss certain ways in which this problem can be solved.

Method #1 : Using list comprehension + split() + strip() 
The combination of the above methods can be used to solve this problem. In this, we perform the split of elements and then strip the stray character to convert data types and compile the whole logic of list construction using list comprehension.

Python3




# Python3 code to demonstrate working of
# Convert String of Heterogeneous types to List
# using list comprehension + split() + strip()
 
# initializing string
test_str = "'gfg', 'is', True, 'best', False"
 
# printing original string
print("The original string is : " + test_str)
 
# Convert String of Heterogeneous types to List
# using list comprehension + split() + strip()
res = [ele.strip() if ele.strip().startswith("'") else ele == 'True'
      for ele in test_str.split(', ')]
 
# printing result
print("List after conversion from string : " + str(res))


Output

The original string is : 'gfg', 'is', True, 'best', False
List after conversion from string : ["'gfg'", "'is'", True, "'best'", False]

Time Complexity: O(n)
Auxiliary Space: O(n)

Method #2 : Using eval() 
This inbuilt function auto-detects the data type and performs the conversion. It is single phrase solution and also provides a solution even if integers are in string and hence recommended for this solution.

Python3




# Python3 code to demonstrate working of
# Convert String of Heterogeneous types to List
# using eval()
 
# initializing string
test_str = "'gfg', 'is', True, 'best', False, 1, 2"
 
# printing original string
print("The original string is : " + test_str)
 
# Convert String of Heterogeneous types to List
# using eval()
res = list(eval(test_str))
 
# printing result
print("List after conversion from string : " + str(res))


Output

The original string is : 'gfg', 'is', True, 'best', False, 1, 2
List after conversion from string : ['gfg', 'is', True, 'best', False, 1, 2]

Time Complexity: O(n)
Auxiliary Space: O(n)

Method : Using ast

Another method to convert a heterogeneous type string to a list is to use the ast module. The ast module provides a function called “literal_eval()” that can be used to evaluate a string containing a Python literal, such as a list or a tuple. This method is safer than using the “eval()” function, as it only evaluates literals and not arbitrary expressions, making it less susceptible to malicious input.

Here is an example of how to use the ast module to convert a heterogeneous type string to a list:

Python3




import ast
 
# Initializing string
test_str = "'gfg', 'is', True, 'best', False, 1, 2"
 
# Printing original string
print("The original string is : " + test_str)
 
# Convert String of Heterogeneous types to List
# using ast.literal_eval()
res = ast.literal_eval(test_str)
 
# Printing result
print("List after conversion from string : ", list(res))
#this code is contributed by edula vinay kumar reddy


Output

The original string is : 'gfg', 'is', True, 'best', False, 1, 2
List after conversion from string :  ['gfg', 'is', True, 'best', False, 1, 2]

Time Complexity: O(n)
Auxiliary Space: O(n)

Method #4: Using Regular Expressions

Use regular expressions to extract the elements from the input string and convert them to a list. 

Here’s the step-by-step approach:

  • Import the re module to use regular expressions.
  • Define the regular expression pattern to match any string enclosed in single quotes, or the boolean values “True” or “False“.
  • Use the re.findall() method to extract all matches of the pattern from the input string.
  • Iterate through the matches and convert them to their appropriate Python data types (i.e. boolean values or strings).
  • Append the converted elements to a list.

Below is the implementation of the above approach:

Python3




import re
 
# initializing string
test_str = "'gfg', 'is', True, 'best', False"
 
# printing original string
print("The original string is : " + test_str)
 
# Using regular expressions
# to convert string to list
pattern = r"'([^']*)'|(\bTrue\b)|(\bFalse\b)"
matches = re.findall(pattern, test_str)
res = []
for match in matches:
    if match[0]:
        res.append(match[0])
    elif match[1]:
        res.append(True)
    elif match[2]:
        res.append(False)
 
# printing result
print("List after conversion from string : " + str(res))


Output

The original string is : 'gfg', 'is', True, 'best', False
List after conversion from string : ['gfg', 'is', True, 'best', False]

Time complexity: The time complexity of this method is O(n), where n is the length of the input string, since we need to perform a constant amount of work for each element in the resulting list.
Auxiliary space: The space complexity of this method is also O(n), since we need to store the resulting list in memory. Additionally, the regular expression pattern and the matches list also consume memory proportional to the length of the input string.

Method 5: using re.findall()

Step-by-step approach:

  • Import the re module.
  • Initialize the input string test_str.
  • Use the re.findall() function to extract all the elements from the input string that match the given regular expression pattern. The pattern matches all strings enclosed in single quotes, as well as the Boolean values True and False.
  • Assign the result to the res variable.
  • Print the result.

Below is the implementation of the above approach:

Python3




import re
 
# initializing string
test_str = "'gfg', 'is', True, 'best', False"
 
# printing original string
print("The original string is : " + test_str)
 
# Convert String of Heterogeneous types to List
# using re.findall()
res = re.findall(r"'(?:[^'\\]|\\.)*'|True|False", test_str)
 
# printing result
print("List after conversion from string : " + str(res))


Output

The original string is : 'gfg', 'is', True, 'best', False
List after conversion from string : ["'gfg'", "'is'", 'True', "'best'", 'False']

Time complexity: The re.findall() method has a linear time complexity of O(n) where n is the length of the input string.
Auxiliary space: This method requires additional space to store the regular expression pattern and the list of extracted elements. The space complexity is O(n) where n is the length of the input string.

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments