How to convert string representation of list to a list
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In Python, the right way to convert a string that looks like a list depends on what format the string actually uses. Some inputs are valid Python literals, some are valid JSON arrays, and some are just comma-separated text that happens to look list-like. The safest solution is to parse according to the real format instead of reaching for eval.
Use ast.literal_eval For Python Literal Syntax
If the string is a valid Python literal such as "[1, 2, 3]" or "['a', 'b']", ast.literal_eval is usually the best answer.
This works because literal_eval can parse safe Python literal structures such as lists, strings, numbers, tuples, dicts, booleans, and None. It does not execute arbitrary code.
Use json.loads For JSON Arrays
If the source string comes from an API or a file that uses JSON, use the JSON parser instead.
This is the correct tool when the input is JSON, but it has JSON rules, not Python rules. For example, JSON uses true, false, and null, while Python literals use True, False, and None.
Do Not Use eval On Untrusted Input
eval can appear to solve the problem quickly:
But this is dangerous if the input is not fully trusted, because eval can execute arbitrary Python code instead of just parsing data. That makes it the wrong default for user input, files, HTTP payloads, and logs.
In practice, if you are considering eval, the better question is usually "what format is this string really supposed to be?"
Sometimes The Data Is Not Really A List Literal
A lot of strings only look list-like at first glance. For example:
That is not a Python list literal or a JSON array. It is just comma-separated text, so simple splitting is more appropriate.
If whitespace is inconsistent, normalize it:
This is often the cleanest solution for lightweight input formats.
Handle Numeric Lists Carefully
If the input is comma-separated numbers rather than a literal list, convert the split strings into the type you actually need.
This is clearer than forcing the data through a parser designed for a different syntax.
A Small Utility Function
If your program receives mixed inputs, write a parser that makes the choice explicit.
This keeps the parsing rules visible instead of hiding them in an unsafe shortcut.
Choose The Parser Based On The Source
A useful rule of thumb is:
- use
json.loadsfor JSON, - use
ast.literal_evalfor trusted Python-literal text, - use
splitfor simple delimiter-based strings.
That small distinction prevents a lot of avoidable bugs.
Common Pitfalls
- Using
evalon untrusted input because it seems convenient. - Passing Python-style strings into
json.loadsand expecting them to parse. - Using
ast.literal_evalwhen the input is really just comma-separated text. - Forgetting to strip whitespace after splitting delimiter-based data.
- Converting to a list successfully but leaving elements as strings when numbers were needed.
Summary
- Use
ast.literal_evalfor strings that contain Python list literals. - Use
json.loadswhen the input is valid JSON. - Use string splitting for simple delimiter-based formats.
- Avoid
evalunless you completely control the input and understand the risk. - The correct parser depends on the real format of the string, not on how list-like it appears.

