
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Convert string enclosed list to list in Python
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 eval
We 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.
Example
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))
Output
Running 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_eval
In 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.
Example
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))
Output
Running 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.loads
The loads function injection module can do a similar conversion where the string is evaluated and actual Python list is generated.
Example
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))
Output
Running 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]
- Related Articles
- Convert list of string to list of list in Python
- Python - Convert list of string to list of list
- Convert list of numerical string to list of Integers in Python
- How to convert list to string in Python?
- Convert a list to string in Python program
- Convert a string representation of list into list in Python
- Python program to convert a list to string
- Convert list of string into sorted list of integer in Python
- Convert list of tuples to list of list in Python
- Python - Convert List to custom overlapping nested list
- Program to convert List of Integer to List of String in Java
- Program to convert List of String to List of Integer in Java
- How to convert a string to a list of words in python?
- Convert list of strings to list of tuples in Python
- Convert list of tuples to list of strings in Python
