Python | Convert heterogenous 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 string contains elements from different data types like boolean. This problem can occur in domains in which 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 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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of
# Convert String of Heterogenous 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 Heterogenous 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))

chevron_right


Output :



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

 

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

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of
# Convert String of Heterogenous 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 Heterogenous types to List
# using eval()
res = list(eval(test_str))
  
# printing result
print("List after conversion from string : " + str(res))

chevron_right


Output :

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



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.