Convert string representation of List to List in Python

avatar

Borislav Hadzhiev

Last updated: Aug 30, 2022

banner

Photo from Unsplash

Convert string representation of List to List in Python #

Use the ast.literal_eval() method to convert the string representation of a list to a list, e.g. my_list = ast.literal_eval(my_str). The ast.literal_eval() method allows us to safely evaluate a string that contains a Python literal.

main.py
from ast import literal_eval my_str = '[1,2,3,4]' # ✅ convert string representation of list to list (ast.literal_eval()) my_list = literal_eval(my_str) print(my_list) # 👉️ [1, 2, 3, 4] print(type(my_list)) # 👉️ <class 'list'> # --------------------------------- # ✅ convert string representation of list to list (using split()) my_list = [int(item) for item in my_str.strip('[]').split(',')] print(my_list) # 👉️ [1, 2, 3, 4] # --------------------------------- # 👇️ for comma-separated string my_str = "1,2,3,4" my_list = [int(digit) for digit in my_str.split(",")] print(my_list) # 👉️ [1, 2, 3, 4]

The first example uses the ast.literal_eval() method to convert the string representation of a list to an actual list object.

main.py
from ast import literal_eval my_str = '[1,2,3,4]' my_list = literal_eval(my_str) print(my_list) # 👉️ [1, 2, 3, 4] print(type(my_list)) # 👉️ <class 'list'>

The ast.literal_eval method allows us to safely evaluate a string that contains a Python literal.

The string may consist of strings, bytes, numbers, tuples, lists, dicts, sets, booleans and None.

If you need to convert a comma-separated string to a list, use the str.split() method.

To convert a comma-separated string to a list:

  1. Use the str.split() method to split the string on each comma.
  2. If working with a numeric list, convert each item to an integer.
main.py
my_str = "1,2,3,4" my_list = [int(digit) for digit in my_str.split(",")] print(my_list) # 👉️ [1, 2, 3, 4]
If the elements in your string are separated with a comma followed by a space, make sure to adjust the argument to the split method to my_str.split(", ").

The first step is to split the string on each comma.

main.py
my_str = "1,2,3,4" print(my_str.split(',')) # 👉️ ['1', '2', '3', '4'] my_str = '1, 2, 3, 4' print(my_str.split(', ')) # 👉️ ['1', '2', '3', '4']

If working with numeric lists, use a list comprehension to convert each string to an integer.

If you have a list of strings, all you have to do is use the str.split() method.

If your string contains brackets, use the str.strip() method to remove them before splitting on each comma.

main.py
my_str = '[1,2,3,4]' my_list = [int(item) for item in my_str.strip('[]').split(',')] print(my_list) # 👉️ [1, 2, 3, 4]

The str.strip method returns a copy of the string with the specified leading and trailing characters removed.

If you have a string containing quoted elements, use the str.replace() method to remove the unnecessary quotes.

main.py
my_str = '["a", "b", "c"]' my_list = my_str.strip('[]').replace('"', '').split(', ') print(my_list) # 👉️ ['a', 'b', 'c']
We used the str.replace() method to remove the double quotes from the string before calling the str.split() method.

If you have a valid JSON string, you can also use the json.loads() method to convert the string representation of the list to a native Python list.

main.py
import json my_str = '["a", "b", "c"]' my_list = json.loads(my_str) print(my_list) # 👉️ ['a', 'b', 'c'] print(type(my_list)) # 👉️ <class 'list'>

The json.loads method parses a JSON string into a native Python object.

If the data being parsed is not a valid JSON string, a JSONDecodeError is raised.

Note that this approach only works if you have a valid JSON string.

If there are nested strings wrapped in single quotes, this wouldn't work.

In that case, you should use the str.replace() method to remove the unnecessary quotes before splitting the string on each comma.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.