ID : 34
viewed : 133
Tags : PythonPython StringPython List
99
In this tutorial, we are going to learn the methods to parse a string to a list in Python.
str.split()
Function in PythonIf in a scenario, we have a string representation of a list like '[ "A","B","C" , " D"]'
and want to convert that representation into an actual list of strings, we can use the str.split()
function to split the string on the basis of each ,
. The str.split()
function takes a delimiter/separator as an input parameter, splits the calling string based on the delimiter, and returns a list of substrings. The code sample below shows us how to parse a string representation of a list into an actual list with the str.split()
function.
stringlist = '[ "A","B","C" , " D"]' print(stringlist.split(","))
Output:
['[ "A"', '"B"', '"C" ', ' " D"]']
We converted the stringlist
string into a list by splitting it based on ,
with the stringlist.split(",")
function. As is apparent from the output, this approach has several problems and does not properly meet our requirements.
str.strip()
Function in PythonTo further convert a string like this into a list, we can use the . This str.strip()
function also takes the delimiter/separator as an input parameter, strips the calling string based on the delimiter, and returns a list of much cleaner substrings. The sample code below shows us how to parse a string representation of a list into an actual list with the str.strip()
function.
stringlist = '[ "A","B","C" , " D"]' print(stringlist.strip(","))
Output:
[ "A","B","C" , " D"]
We converted the stringlist
string into a list by splitting it on the basis of ,
with the stringlist.split(",")
function. We get a much cleaner list of strings this time around. The only disadvantage of this approach is that there are some unwanted blank spaces like the space in the fourth element of the list.
json.loads()
Function in PythonWe can also use the json
module for our specific problem. The takes a JSON object as parameter, deserializes JSON object, and returns the results in a list. The JSON object parameter, in this case, can also be a string. The sample code below shows us how to parse a string representation of a list into an actual list with the json.loads()
function.
import json stringlist = '[ "A","B","C" , " D"]' print(json.loads(stringlist))
Output:
['A', 'B', 'C', ' D']
We converted our stringlist
string into a cleaner list with the json.loads(stringlist)
function in Python. The only difference between the json.loads()
function and our previous approaches is that we don’t have to specify any delimiter or separator character here. The json.loads()
function automatically determines the delimiter for us. This method also contains the problem of unwanted blank spaces.
ast.literal_eval()
Function in PythonOne other method to solve our specific problem is the ast
module. The takes a string representation of a Python literal structure like tuples, dictionaries, lists, and sets. If we pass the string into that literal structure, it returns the results. In our case, we have a string representation of a list. So, the ast.literal_eval()
function takes this string, parses it into a list, and returns the results. The following code snippet shows us how to parse a string representation of a list into an actual list with the ast.literal_eval()
function.
import ast stringlist = '[ "A","B","C" , " D"]' print(ast.literal_eval(stringlist))
Output:
['A', 'B', 'C', ' D']
We converted the stringlist
string into a cleaner list with the ast.literal_eval()
function in Python. Similar to the previous approach, we don’t have to specify a delimiter or a separator. Also similar to the previous approach, this method has the same problem of unwanted blank spaces. But these blank spaces can be easily removed.