To convert a string to a list in Python, you can use the string split() method. The split() method splits the strings, stores them in the list, and returns a list of the words using the “delimiter” as the delimiter string.
Example
def stringToList(string):
listRes = list(string.split(" "))
return listRes
strA = "Millie Bobby Brown"
print(stringToList(strA))
Output
['Millie', 'Bobby', 'Brown']
You can check the data type using the type() function.
def stringToList(string):
listRes = list(string.split(" "))
return listRes
strA = "Millie Bobby Brown"
print(type(stringToList(strA)))
Output
<class 'list'>
If the delimiter is not specified in the function argument or is None, then a different splitting algorithm is applied: its runs of consecutive whitespace are regarded as the single separator.
Alternate approaches
Solution 1: Converting string to a list using strip() and split()
The strip() method returns the copy of a string with both leading and trailing characters removed based on the string argument passed.
The strip() method removes characters from both left and right based on the argument.
initial_list = "[11, 21, 29, 46, 19]"
print ("initial string", initial_list)
print (type(initial_list))
op = initial_list.strip('][').split(', ')
print ("final list", op)
print (type(op))
Output
initial string [11, 21, 29, 46, 19]
<class 'str'>
final list ['11', '21', '29', '46', '19']
<class 'list'>
Here, we defined a string that looks like a list.
Then we use the strip() and split() method to convert the string to a list, and finally, we print the list and its type for double-checking.
Solution 2: Converting using AST(Abstract Syntax Trees) module
Python ast module helps Python applications process the trees of abstract syntax grammar.
The abstract syntax might change with each Python release; this module helps determine programmatically what the current grammar looks like.
The abstract syntax tree can be generated by passing the ast.
PyCF_ONLY_AST as the flag to the compile() built-in function or using a parse() helper function provided in this module.
The result will be a tree of objects whose classes inherit from the ast module.
import ast
# initializing string representation of a list
ini_list = "[11, 21, 19, 46, 29]"
# printing intialized string of list and its type
print("initial string", ini_list)
print(type(ini_list))
# Converting string to list
res = ast.literal_eval(ini_list)
# printing final result and its type
print("final list", res)
print(type(res))
Output
initial string [11, 21, 19, 46, 29]
<class 'str'>
final list [11, 21, 19, 46, 29]
<class 'list'>
Solution 3: Converting string to the list using json.loads()
There is a third way to convert the Python string to a list using json.loads() method.
import json
# initializing string representation of a list
initial_list = "[11, 21, 19, 46, 29]"
# printing intialized string of list and its type
print("initial string", initial_list)
print(type(initial_list))
# Converting string to list
op = json.loads(initial_list)
# printing final result and its type
print("final list", op)
print(type(op))
Output
initial string [11, 21, 19, 46, 29]
<class 'str'>
final list [11, 21, 19, 46, 29]
<class 'list'>
First, we must import a json module and then use the json.loads() method to convert the string to a list format.
Conclusion
Converting a string to a list in Python can be done in multiple ways. The easiest way to do this is using the split() method. The split() method splits a string into a list using a specified separator string as the delimiter.