How to Convert String Representations of Lists to Lists in Python
This guide explains how to convert a string that looks like a Python list (e.g., "[1, 2, 3]"
) into an actual Python list object. We'll cover the safest and most reliable methods (ast.literal_eval()
, json.loads()
), as well as less robust (but sometimes useful) techniques like string manipulation and yaml.safe_load()
. We will explicitly discuss the security risks of using eval()
.
Using ast.literal_eval()
(Recommended)
The ast.literal_eval()
function from the ast
(Abstract Syntax Trees) module is the safest and most recommended way to convert a string representation of a Python literal (including lists, tuples, dictionaries, numbers, strings, booleans, and None
) into its corresponding Python object:
from ast import literal_eval
my_str = '[1, 2, 3, 4]'
my_list = literal_eval(my_str)
print(my_list) # Output: [1, 2, 3, 4]
print(type(my_list)) # Output: <class 'list'>
- Safety:
literal_eval()
only evaluates valid Python literal structures. It won't execute arbitrary code, making it much safer than the built-ineval()
function. - Flexibility: It handles various literal types, not just lists.
- It will throw an exception if the string is not a valid representation of a Python literal.
Using json.loads()
(for Valid JSON)
If your string is a valid JSON array, you can use json.loads()
:
import json
my_str = '["a", "b", "c"]' # Valid JSON array
my_list = json.loads(my_str)
print(my_list) # Output: ['a', 'b', 'c']
print(type(my_list)) # Output: <class 'list'>
my_str = '[1, 2, 3, 4]' # JSON containing numbers
my_list = json.loads(my_str)
print(my_list) # Output: [1, 2, 3, 4]
json.loads()
parses a JSON string and converts it into the equivalent Python object.- JSON Compatibility: This method only works if the string is valid JSON. JSON requires double quotes around strings, and it doesn't allow trailing commas.
Using String Manipulation (Least Recommended)
You can use string manipulation techniques (strip()
, replace()
, split()
), but this is error-prone and not recommended unless you have very tight control over the input string format.
Removing Brackets and Splitting
my_str = '[1,2,3,4]'
my_list = [int(item) for item in my_str.strip('[]').split(',')]
print(my_list) # Output: [1, 2, 3, 4]
.strip('[]')
: Removes the leading[
and trailing]
characters..split(',')
: Splits the string into a list of strings, using the comma as a delimiter.- The list comprehension iterates through the elements and converts them to integers.
Handling Quoted Strings
If your string contains quoted elements, you'll need to remove the quotes as well:
my_str = '["a", "b", "c"]'
my_list = my_str.strip('[]').replace('"', '').split(', ')
print(my_list) # Output: ['a', 'b', 'c']
- The
replace('"', '')
removes all double quotes.
Fragility: These string manipulation methods are very fragile. They break easily if the input string doesn't exactly match the expected format (extra spaces, different quotes, etc.). ast.literal_eval()
and json.loads()
are far more robust.
Using re.findall()
(for specific patterns)
The re.findall()
method finds all the occurrences of the pattern and returns a list:
import re
my_str = '[1, 2, 3, 4]'
list_of_strings = re.findall(r'[0-9]+', my_str)
print(list_of_strings) # Output: ['1', '2', '3', '4']
list_of_integers = [int(item) for item in list_of_strings]
print(list_of_integers) # Output: [1, 2, 3, 4]
# For strings
my_str = '["ab", "cd", "ef", "gh"]'
list_of_strings = re.findall(r'\w+', my_str)
print(list_of_strings) # Output: ['ab', 'cd', 'ef', 'gh']
Using PyYAML
import yaml
my_str = '[1, 2, 3, 4]'
my_list = yaml.safe_load(my_str) # Avoid using yaml.load or yaml.full_load
print(my_list) # Output: [1, 2, 3, 4]
- The
yaml.safe_load()
should always be preferred overyaml.load()
oryaml.full_load()
, to avoid running into security issues. - The method will parse the string and convert it to a Python list.
Using eval()
(Strongly Discouraged)
While eval()
can convert a string representation of a list to a list, it's extremely dangerous and should almost never be used:
my_str = '[1, 2, 3, 4]'
# ⛔️ DANGEROUS! DO NOT USE eval() WITH UNTRUSTED INPUT!
# my_list = eval(my_str)
# print(my_list)
Security Risk: eval()
executes arbitrary Python code contained in the string. If my_str
comes from user input or an external source, a malicious user could inject code to compromise your system.
Never use eval()
with untrusted input.
Using map()
You can use the map
function to convert a string representation of a list of numbers to a list of numbers:
my_str = '[1, 2, 3, 4]'
my_list = list(map(int, my_str[1:-1].split(',')))
print(my_list) # Output: [1, 2, 3, 4]
my_str[1:-1]
slices the string to remove the bracketssplit(',')
separates the string into a list of stringsmap(int, ...)
converts each element of the string into an integer.- The result is converted to a list using the
list()
constructor. - This approach does not work directly with strings.