Skip to content
Related Articles
Open in App
Not now

Related Articles

Python | Convert a string representation of list into list

Improve Article
Save Article
  • Last Updated : 07 Feb, 2023
Improve Article
Save Article

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 are quite common in web development. Let’s discuss certain ways in which this can be performed. 

Method #1: Using split() and strip() 

Python3




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

Output:

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

  Method #2: Using ast.literal_eval() 

Python3




# 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 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'>

  Method #3: Using json.loads() 

Python3




# 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 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'>

Method #4: Using re

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

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]
#This code is contributed by Edula Vinay Kumar Reddy

Output

[1, 2, 3, 4, 5]

The time complexity of this approach is O(n), where n is the number of elements in the list. The space complexity is also O(n), since the elements of the list are stored in a new list.

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

Method #5: Using eval

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))
#This code is contributed Vinay Pinjala.

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)


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!