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.
The original string is : 'gfg', 'is', True, 'best', False List after conversion from string : ["'gfg'", "'is'", True, "'best'", False]
Method #2 : Using
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.
The original string is : 'gfg', 'is', True, 'best', False, 1, 2 List after conversion from string : ["'gfg'", "'is'", True, "'best'", False, 1, 2]
- Python - String concatenation in Heterogenous list
- Python | Summation of integers in heterogenous list
- Python | Convert mixed data types tuple list to string list
- Python | Convert list of string into sorted list of integer
- Python Program to convert List of Integer to List of String
- Python | Convert string List to Nested Character List
- Python | Convert list of numerical string to list of Integers
- Python | Convert string enclosed list to list
- Python | Convert a string representation of list into list
- Python | Convert list of string to list of list
- Python | Convert List of String List to String List
- Python | Convert String to tuple list
- Python | Convert a list of characters into a string
- Python program to convert a list to string
- Python | Program to convert String to a List
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.