Eval list of strings python
There isn't any need to import anything or to evaluate. You can do this in one line for most basic use cases, including the one given in the original question. Show One liner
Explanation
Outputs:
You can parse and clean up this list as needed using list comprehension.
Nested listsIf you have nested lists, it does get a bit more annoying. Without using regex (which would simplify the replace), and assuming you want to return a flattened list (and the zen of python says flat is better than nested):
If you need to retain the nested list it gets a bit uglier, but it can still be done just with regular expressions and list comprehension:
This last solution will work on any list stored as a string, nested or not. We may sometime get data which contains strings but the structure of the data inside the stream is a Python list. In this article we will convert string enclosed list to an actual Python list which can be further used in data manipulation. With evalWe know the eval function will give us the actual result which is supplied to it as parameter. So so we supplied the given string to the eval function and get back the Python list. ExampleLive Demo stringA = "['Mon', 2,'Tue', 4, 'Wed',3]" # Given string print("Given string : \n",stringA) # Type check print(type(stringA)) # using eval res = eval(stringA) # Result print("The converted list : \n",res) # Type check print(type(res)) OutputRunning the above code gives us the following result − Given string : ['Mon', 2,'Tue', 4, 'Wed',3] The converted list : ['Mon', 2, 'Tue', 4, 'Wed', 3] With ast.literal_evalIn this approach, we take the estimate and use the literal_eval function by giving it the string as a parameter. It gives back the Python list. ExampleLive Demo import ast stringA = "['Mon', 2,'Tue', 4, 'Wed',3]" # Given string print("Given string : \n",stringA) # Type check print(type(stringA)) # using literal_eval res = ast.literal_eval(stringA) # Result print("The converted list : \n",res) # Type check print(type(res)) OutputRunning the above code gives us the following result − Given string : ['Mon', 2,'Tue', 4, 'Wed',3] The converted list : ['Mon', 2, 'Tue', 4, 'Wed', 3] With json.loadsThe loads function injection module can do a similar conversion where the string is evaluated and actual Python list is generated. ExampleLive Demo import json stringA = '["Mon", 2,"Tue", 4, "Wed",3]' # Given string print("Given string : \n",stringA) # Type check print(type(stringA)) # using loads res = json.loads(stringA) # Result print("The converted list : \n",res) # Type check print(type(res)) OutputRunning the above code gives us the following result − Given string : ["Mon", 2,"Tue", 4, "Wed",3] The converted list : ['Mon', 2, 'Tue', 4, 'Wed', 3]
Updated on 04-Jun-2020 11:11:47
How do you evaluate a string list in Python?Convert String to List in Python Using the list() Function. The list() function converts the string into a list in Python. The list() function takes a string as an argument and converts it into a list. Every character in the string becomes the individual element in the list.
How do you evaluate a list in Python?First, we define a string, that carries the syntax of a list. Next, we use eval to evaluate it. Finally, we can show that it has the properties of a Python list. Another example, where we allow the user to input the string to be evaluated.
How do you parse a string in Python?Use str.
Call str. split(sep) to parse the string str by the delimeter sep into a list of strings. Call str. split(sep, maxsplit) and state the maxsplit parameter to specify the maximum number of splits to perform.
What is the reliable method for transforming a string to a list?The split() method is the recommended and most common method used to convert string to list in Python.
|