Open In App
Related Articles

Python | Convert a string representation of list into list

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

Many times, we come across 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 a list in Python to perform tasks is quite common in web development.

Convert a string of a list into a list

Below is the list of methods that we will use in this article:

Convert a string representation of list into a list using split() and strip() 

It then converts this string representation into an actual list using the strip and split methods, removing the brackets and splitting the elements. The resulting list, res, is printed along with its type. The code demonstrates the transformation from a string representing a list to an actual list by removing the brackets and splitting the elements based on commas.

Python3

# string representation of list to list using strip and split
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))

                    

Output
initial string [1, 2, 3, 4, 5]
<class 'str'>
final list ['1', '2', '3', '4', '5']
<class 'list'>


Time complexity: O(n), where n is the length of the string representation of the list.
Auxiliary space: O(1).

Convert a string representation of list into list using ast.literal_eval() 

By utilizing the ast.literal_eval() function from the ast module, the string is safely evaluated as Python code, converting it into an actual list. The resulting list res is printed along with its type. The code showcases how to convert a string representation of a list into an actual list using ast.literal_eval(), which safely evaluates the string as Python code.

Python3

# 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 initialized 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))

                    

Output
initial string [1, 2, 3, 4, 5]
<class 'str'>
final list [1, 2, 3, 4, 5]
<class 'list'>


Time complexity: O(n), where n is the length of the input string representation of the list. 
Auxiliary space complexity: O(n), where n is the length of the input string representation of the list. 

Convert a string representation of list into list using json.loads() 

It then converts the string to a list using json.loads(), a function from the json module. The resulting list res is printed along with its type. This code demonstrates how to convert a string representation of a list into an actual list using json.loads()

Python3

# 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 initialized 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))

                    

Output
initial string [1, 2, 3, 4, 5]
<class 'str'>
final list [1, 2, 3, 4, 5]
<class 'list'>


Time complexity: The time complexity of the json.loads() method is linear, O(n), where n is the length of the input string.
Auxiliary space: The auxiliary space complexity of this code is O(n), where n is the length of the input string.

Convert a string representation of list into list using regular expression

Here is an example of how you can use a regular expression to convert a string representation of a list into a list:

Note: This approach assumes that the string representation of the list contains only integers.

Python3

import re
 
ini_list = "[1, 2, 3, 4, 5]"
 
# Extract the elements of the list from the string
elements = re.findall(r'\d+', ini_list)
 
# Convert the elements to integers
res = [int(x) for x in elements]
print(res)  # [1, 2, 3, 4, 5]

                    

Output
[1, 2, 3, 4, 5]


Time complexity: O(n), where n is the number of elements in the list. 
Auxiliary space: O(n), since the elements of the list are stored in a new list.

Convert a string list into list using eval Method

Initializes a string ini_list representing a list and prints it along with its type. It then uses the eval() function to evaluate the string as Python code, converting it to an actual list. The resulting list res is printed along with its type. This approach can be used to convert a string representation of a list into an actual list.

Python3

# 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 = eval(ini_list)
 
# printing final result and its type
print("final list", res)
print(type(res))

                    

Output
initial string [1, 2, 3, 4, 5]
<class 'str'>
final list [1, 2, 3, 4, 5]
<class 'list'>


Time Complexity: O(n)
Auxiliary Space: O(n)

Convert a string representation of list into list using a list and map

Initialize a string variable ini_list with a string representation of a list [1, 2, 3, 4, 5]. Print the initial string representation of the list and its type using the print() function and the type() function respectively and use the list() function and the map() function to convert the string to a list of integers. The map() function takes two arguments: a function (in this case, the int() function) and an iterable (in this case, a list of string representations of integers obtained by slicing the string from index 1 to -1 and then splitting it at the commas). The list() function then converts the resulting map object into a list.

Print the final list and its type using the print() function and the type() function respectively.

Python3

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 = list(map(int, ini_list[1:-1].split(',')))
 
# printing final result and its type
print ("final list", res)
print (type(res))

                    

Output
initial string [1, 2, 3, 4, 5]
<class 'str'>
final list [1, 2, 3, 4, 5]
<class 'list'>


Time Complexity: O(n)
Auxiliary Space: O(n)



Last Updated : 17 Aug, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads