convert string representation to list in python

How to Convert String Representation of List to List

Often we come across string representation of lists that we need to convert to actual lists in python. This is a common problems when you work with data dumps and raw strings that are transferred in web development. We cannot run python list functions on strings that represent lists. We need to them to be actual lists of this purpose. In this article, we will learn several ways to convert string representation of lists into lists in python.


How to Convert String Representation of List to List

Here are the different ways to convert string representation into lists.

1. Using split() and strip()

In this approach, we use strip function to remove the square brackets at the ends of string representation of lists. Then we use split() function to split the remaining string by comma, into an array that is basically a list in python.

Here is an example. Let us say you have the following string representation of list.

ini_list = "[1, 2, 3, 4, 5]"

Here is how to wen convert it into actual list using strip() and split() functions.

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

When you run the above code, you will see the following output.

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


2. Using json.loads()

You can even treat the raw string as a JSON data and use json functions to convert them into python lists. Python provides a JSON library that allows you to easily convert strings to python objects such as lists, dictionaries, tuples, etc using json.loads() function. Let us say you have the following data.

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

In the above code, we import python’s json library and call json.loads() function on the input string directly. It returns a python list. Finally, we print both string and list. This method is more concise that the previous method and does everything using a single function json.loads().


3. Using ast.literal_eval()

You can also use literal_eval() function in python’s ast library. It allows you to easily convert string into list. Here is an example.

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

In the above code, we import ast library, initialize input string and use literal_eval() function to convert it into python list.

In this article, we have learnt 3 different ways to convert string representation of list into list. Out of them, the most recommended way is to use JSON library as mentioned in #2 because in most web applications, websites and mobile applications, data is transferred using JSON format, so you will be easily able to use it for converting string to list and even vice versa.

Also read:

How to Set Current Directory to Directory of Shell Script
How to Change Shutdown Message in Linux
How to Mkdir Only If Directory Doesn’t exist
How to Create Nested Directory in Linux With Single Command
How to Create CA Bundle from CRT Files for SSL Certificate

Leave a Reply

Your email address will not be published.