Python | Convert a string representation of list into list
Many times, we come over the dumped data that is found in the string format and we require it to be represented into the actual list format in which it was actually found. This kind of problem of converting a list represented in string format back to list to perform tasks is quite common in web development. Let’s discuss certain ways in which this can be performed.
Method #1: Using split() and strip()
# Python code to demonstrate converting # string representation of list to list # using strip and split # initializing string representation of a list ini_list = "[1, 2, 3, 4, 5]" # printing initialized string of list and its type print ("initial string", ini_list) print (type(ini_list)) # Converting string to list res = ini_list.strip('][').split(', ') # printing final result and its type print ("final list", res) print (type(res)) |
initial string [1, 2, 3, 4, 5] <class 'str'> final list ['1', '2', '3', '4', '5'] <class 'list'>
Method #2: Using ast.literal_eval()
# Python code to demonstrate converting # string representation of list to list # using ast.literal_eval() import ast # initializing string representation of a list ini_list = "[1, 2, 3, 4, 5]" # 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)) |
initial string [1, 2, 3, 4, 5] <class 'str'> final list [1, 2, 3, 4, 5] <class 'list'>
Method #3: Using json.loads()
# Python code to demonstrate converting # string representation of list to list # using json.loads() import json # initializing string representation of a list ini_list = "[1, 2, 3, 4, 5]" # printing intialized string of list and its type print ("initial string", ini_list) print (type(ini_list)) # Converting string to list res = json.loads(ini_list) # printing final result and its type print ("final list", res) print (type(res)) |
initial string [1, 2, 3, 4, 5] <class 'str'> final list [1, 2, 3, 4, 5] <class 'list'>
Recommended Posts:
- Python | Convert list of string to list of list
- Python | Convert list of numerical string to list of Integers
- Python | Convert list of string into sorted list of integer
- Python | Convert string enclosed list to list
- Python | Convert list of tuples to list of list
- Python program to convert a list to string
- Python | Program to convert String to a List
- Python | Convert a list of characters into a string
- Python | Ways to convert list of ASCII value to string
- Python | Convert list of strings to space separated string
- Python | Convert a nested list into a flat list
- Python | Convert list of strings to list of tuples
- Python | Convert 1D list to 2D list of variable length
- Python | Convert list of tuples to list of strings
- Python | Ways to Convert a 3D list into a 2D 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 contribute@geeksforgeeks.org. 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.



