Convert String representation of List to a List in Python

In this tutorial, we will learn about different ways through which we can convert a given String representation of a list to a list object in Python.

Table Of Contents

But first what is What is String Representation of a list?

Here is the difference in below example:

Code :

# String Representation of a list
str_lst = "[MSD,VK,RS]"

# It will print the data type of var str_lst
# which is of <class 'str'>
print( type(str_lst) )

Output :

<class 'str'>

Now you can see how a string representation of a list looks like. We learn about some methods in Python through which we can convert string representaion of a list to a list.

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

The split() method of class string is used to split the given string to a list of substrings based on a separator. Whereas the strip() method of class string is used to remove spaces or given characters from the beginning and from the end of string. Here we will be using combination of these two methods, to convert the string representation of a list to a list object.

First we will remove everything before and after the square braces and then use split() method. It will convert the given string representation of a list to a list of substrings. See this Example Code below.

CODE :

# String representation of list
str_lst = "[MSD,VK,RS]"

# It will print the data type of var
# str_lst which is of <class 'str'>
print(type(str_lst))  

# Strip the open close brackets from string and 
# then split the string by using comma as delimeter 
new_lst = str_lst.strip('][').split(', ')

print(new_lst)

# this will print the type of var new_lst 
# which happens to be of class list
print(type(new_lst))

OUTPUT :

<class 'str'>

['MSD,VK,RS']
<class 'list'>

Here, in code example above, we have successfully converted the string representation of a list into list object using a combination of split() and strip() methods in Python.

Method 2 : Using ast.literal_eval()

Next method that we will be using is the literal_eval() method of ast (Abstract Syntax Tree) module, which comes bundled with Python. The literal_eval() recieves only one parameter which is the string, that needs to be evaluated and returns an oject represented by string. We can use this to convert the string representation of list to a list object.

See the example code below :

CODE :

import ast

str_lst = "[7,18,45]"

# It will print the data type of var str_lst
# which is of <class 'str'>
print(type(str_lst))

# Convert string representation of list into a list
new_lst = ast.literal_eval(str_lst) 

print(new_lst)

# It will print the type of var new_lst
# which happens to be of class list
print(type(new_lst))

OUTPUT :

<class 'str'>

[7, 18, 45]
<class 'list'>

As you can see in code and output above, we have successfully converted string representation of a list into list using literal_eval() method. This method will not work if items in list are of string class as it will throw an error ValueError. So this function must be used with int or float class types.

Method 3 : Using json.loads()

Next method we will be using is loads() method of the json module in Python. The json stands for Javascript Object Notation, which is a language independent format used to store data.

The loads() method recieves five parameters :

  • first is string which needs to be parsed.
  • second is object_hook(optional), which is used when parsed string is object literal.
  • third is parse_float(optional), which is used when JSON float needs to be decoded.
  • fourth is parse_int(optional), which is used when JSON int needs to be decoded.
  • object_pairs_hook(optional), which is used when when object literal needs to be decoded.

Here we need to convert the given string only to list, so we will pass only string as an argument. See the example below :

CODE :

import json

str_lst = "[7,18,45]"

# It will print the data type of var str_lst
# which is of <class 'str'>
print(type(str_lst))

# Convert string representation of list into a list
new_lst = json.loads(str_lst)

print(new_lst)

# It will print the type of var new_lst
# which happens to be of class list
print(type(new_lst))

OUTPUT :

<class 'str'>

[7, 18, 45]
<class 'list'>

In code above, you can see the type of var new_lst is of class list, so we have successfully converted the given string representation of the list to list using loads() method of json module.

Summary

In this article, we have seen three different methods through which we can convert string representation of a list to a list using Python Programming language. First method can be used with every data type, in second method you need to provide int or float and in third method you can provide ever data type with specific optional argument to prevent any kind of errors. You can always use any of the methods above according to your need.

Also we have used Python 3.10.1 for writing examples. Type python –version to check your python version. Always try to write & understand example codes. Happy Coding.

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top