Python | Convert list of string to list of list

25 Jun, 2022
Many times, we come over the dumped data that is found in the string format and we require it to be represented in 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 strip() + split() A combination of strip and split function can perform a particular task. The strip function can be used to get rid of the brackets and split function can make the data list comma-separated. 
Python3
# Python3 code to demonstrate# to convert list of string to list of list# using strip() + split() # initializing listtest_list = ['[1, 4, 5]', '[4, 6, 8]'] # printing original listprint (& quot        The original list is : & quot        + str(test_list)) # using strip() + split()# to convert list of string to list of listres = [i.strip(& quot                [] & quot                ).split("                       , & quot                       ) for i in test_list] # printing resultprint (& quot        The list after conversion is : & quot        + str(res))
Output :
The original list is : ['[1, 4, 5]', '[4, 6, 8]']
The list after conversion is : [['1', ' 4', ' 5'], ['4', ' 6', ' 8']]
Method #2 : Using list slicing and split() The task performed in the above method can also be performed using the list slicing in which we slice all the elements from second to second last element hence omitting the last brackets.  
Python3
# Python3 code to demonstrate# to convert the list of string to list of list# using list slicing + split() # initializing listtest_list = ['[1, 4, 5]', '[4, 6, 8]'] # printing original listprint (& quot        The original list is : & quot        + str(test_list)) # using list slicing + split()# to convert list of string to list of listres = [i[1: -1].split(', ') for i in test_list] # printing resultprint (& quot        The list after conversion is : & quot        + str(res))
Output :
The original list is : ['[1, 4, 5]', '[4, 6, 8]']
The list after conversion is : [['1', ' 4', ' 5'], ['4', ' 6', ' 8']]
Method#3 : Using re.findall and list comprehension This task can be performed using list comprehension for iterating over the list and re.findall function to make list with all element that matches pattern. 
Python3
# Python3 code to demonstrate# to convert list of string to list of list# using list comprehension + re.findallimport re# initializing listtest_list = ['[1, 4, 5]', '[4, 6, 8]'] # printing original listprint("The original list is : " + str(test_list)) # using list comprehension + re.findall# to convert list of string to list of listres = [re.findall('[0-9]', i) for i in test_list] # printing resultprint("The list after conversion is : " + str(res))
Output:
The original list is : ['[1, 4, 5]', '[4, 6, 8]']
The list after conversion is : [['1', '4', '5'], ['4', '6', '8']]
Method#4: Using loop + eval() method This task can be performed with the help of this functions. Loop is used to iterate over the list and eval is used to parse the expression present in the string. 
Python3
# Python3 code to demonstrate# to convert list of string to list of list# using loop + eval functionsimport re# initializing listtest_list = ['[1, 4, 5]', '[4, 6, 8]'] # printing original listprint("The original list is : " + str(test_list)) # using loop + eval functions# to convert list of string to list of listfor i in range(len(test_list)):    test_list[i] = eval(test_list[i]) # printing resultprint("The list after conversion is : " + str(test_list))
Output:
The original list is : ['[1, 4, 5]', '[4, 6, 8]']
The list after conversion is : [[1, 4, 5], [4, 6, 8]]
nidhi_biet
satyam00so
Python list-programs
python-list
Python
python-list
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Python Dictionary
Different ways to create Pandas Dataframe
Enumerate() in Python
Python String | replace()
How to Install PIP on Windows ?
*args and **kwargs in Python
Python Classes and Objects
Python OOPs Concepts
Introduction To PYTHON
Convert integer to string in Python