Skip to content
Related Articles

Related Articles

Python | Convert list of string to list of list

View Discussion
Improve Article
Save Article
  • Difficulty Level : Medium
  • Last Updated : 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 list
test_list = ['[1, 4, 5]', '[4, 6, 8]']
 
# printing original list
print (& quot
        The original list is : & quot
        + str(test_list))
 
# using strip() + split()
# to convert list of string to list of list
res = [i.strip(& quot
                [] & quot
                ).split("
                       , & quot
                       ) for i in test_list]
 
# printing result
print (& 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 list
test_list = ['[1, 4, 5]', '[4, 6, 8]']
 
# printing original list
print (& quot
        The original list is : & quot
        + str(test_list))
 
# using list slicing + split()
# to convert list of string to list of list
res = [i[1: -1].split(', ') for i in test_list]
 
# printing result
print (& 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.findall
import re
# initializing list
test_list = ['[1, 4, 5]', '[4, 6, 8]']
 
# printing original list
print("The original list is : " + str(test_list))
 
# using list comprehension + re.findall
# to convert list of string to list of list
res = [re.findall('[0-9]', i) for i in test_list]
 
# printing result
print("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 functions
import re
# initializing list
test_list = ['[1, 4, 5]', '[4, 6, 8]']
 
# printing original list
print("The original list is : " + str(test_list))
 
# using loop + eval functions
# to convert list of string to list of list
for i in range(len(test_list)):
    test_list[i] = eval(test_list[i])
 
# printing result
print("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]]

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!