How to Convert String Representation of List to List in Python?

How to Convert String Representation of List to List in Python?

Many times in the project, the list is stored in the form of a string.

One of the use cases was when I was working on a Django project, I had to save the list of selected multiple-choice options as a string. And then I have to convert it back to the Python list for performing certain operations.

I explored multiple ways of accomplishing this requirement. After doing all these experiments, I think sharing it here will help you.

Convert String Representation of List to List in Python

Let’s see the various methods to convert the string representation of the list to list in Python.

1. Using split()

The method split() is a very know Python string method that converts the string into the list.

This is a manual method of converting the string representation of string to the list.

sample_str = "[34, 54, 3]"

# print sample string
print ("initial string", sample_str)
print (type(sample_str))

# convert string to Python list
out_list = sample_str[1:-1].split(', ')

# print list as output
print ("final list", out_list)
print (type(out_list))

Output:

initial string [34, 54, 3]
<class 'str'>
final list [34, 54, 3]
<class 'list'>

Drawbacks:

  • You have to be very cautious about the spaces. If the spoace ius missing between two item in the list, this method wioll not work as expected.
  • If there are strings in the list, it adds extra quote for the strings.

Now we will see the other two methods to convert a string into a list without using the split() method.

2. Using json

The json library comes pre-installed with Python. You don’t need to install it explicitly. Just import it and use it.

import json

sample_str = "[34, 54, 3]"

# print sample string
print ("initial string", sample_str)
print (type(sample_str))
  
# convert string to Python list
out_list = json.loads(sample_str)
  
# print list as output
print ("final list", out_list)
print (type(out_list))

Output:

initial string [34, 54, 3]
<class 'str'>
final list [34, 54, 3]
<class 'list'>

Drawbacks:

If there string representation of the list contains another string, it prompts an error.

3. Using ast

Like json library, ast library also comes preinstalled with Python. You don’t need to install it explicitly. Just import it and use it.

This is the standard method to convert the string representation of a list to a Python list. And this also works for the list which a string inside.

import ast

sample_str = "[34, 54, 3, 'cse', 'code']"

# print sample string
print ("initial string", sample_str)
print (type(sample_str))
  
# convert string to Python list
out_list = ast.literal_eval(sample_str)
  
# print list as output
print ("final list", out_list)
print (type(out_list))

Output:

initial string [34, 54, 3, 'cse', 'code']
<class 'str'>
final list [34, 54, 3, 'cse', 'code']
<class 'list'>

Once you get the list, you can perform various operations on the Python list.

Verdict and Conclusion

Using the split method is not the standard solution. It is a workaround. Other two methods (json.load() and ast.literal_eval()) are standard. If there is any possibility where string dispensation of the list also contains the string, you can not use json.load().

Using ast.literal_eval() solves all your problems. Go with it. Any query? Let me know in the comment below.

Leave a Reply

Your email address will not be published. Required fields are marked *