HOWTO · Python

How to Convert a String to List in Python

Learn when to use list(), split(), ast.literal_eval(), and list comprehensions to convert Python strings into characters, words, delimited values, or validated list data.

On this page

Choose the method based on what each item in the result should be. Use list(text) for one item per character, text.split() for words or delimiter-separated values, ast.literal_eval(text) for trusted text that already represents a Python literal, and a list comprehension when each item needs filtering or transformation. Do not use eval() on input you do not completely control: it can execute arbitrary Python code.

Convert a string to characters with list()

list() iterates over a string, so it produces one list element for each Unicode character. It does not split a sentence into words.

text = "Hello"
characters = list(text)
print(characters)

Output:

['H', 'e', 'l', 'l', 'o']

Use this when character-level processing is intended. An empty string becomes []; spaces and punctuation are also characters, not separators.

Split words or delimited values with split()

For words separated by arbitrary whitespace, call split() without an argument. Consecutive whitespace is treated as one separator and leading or trailing whitespace is ignored. Pass an explicit separator when the input format defines one.

sentence = "Convert this string to a list"
words = sentence.split()
print(words)

Output:

['Convert', 'this', 'string', 'to', 'a', 'list']

For comma-separated data, use split(",") and strip each field if spaces after commas are allowed:

text = "red, green, blue"
colors = [item.strip() for item in text.split(",")]
print(colors)

Output:

['red', 'green', 'blue']

With an explicit separator, empty fields are preserved, and a separator that is not present leaves the whole string as one item. If the input is CSV with quoted commas, use Python’s csv module instead of extending split().

Use maxsplit when only the first few separators should create boundaries. For example, maxsplit=1 keeps the remainder in the second item:

text = "red,green,blue"
first_two = text.split(",", maxsplit=1)
print(first_two)

Output:

['red', 'green,blue']

Parse text that already represents a list

If the string is a Python-literal representation such as "[1, 2, 3]", use ast.literal_eval(). It accepts literals and containers rather than executing arbitrary expressions, but it still expects valid, trusted-format input and can raise ValueError or SyntaxError for malformed text.

import ast

text = "[1, 2, 3]"
values = ast.literal_eval(text)
if not isinstance(values, list):
    raise TypeError("expected a list literal")
print(values)

Output:

[1, 2, 3]

Do not assume that every successful parse is a list: a string such as "(1, 2)" produces a tuple, which is why the type check is useful. For JSON text, use json.loads() instead; Python literal syntax and JSON are similar but not identical. Never replace literal_eval() with eval() for untrusted input.

Transform or filter items with a comprehension

Use a comprehension when conversion includes a rule, such as keeping only digits or changing case:

text = "P y 3"
digits = [char for char in text if char.isdigit()]
print(digits)

Output:

['3']

The expression can also transform each item, for example [char.lower() for char in text if char.isalpha()]. A comprehension works over characters unless you first call split(); combine the two when you need to normalize words.

Choose the right method

Use list() for characters, split() for a known delimiter, ast.literal_eval() for a validated Python-literal format, and a comprehension for filtering or transforming. Validate external input before parsing, and choose a format-specific parser when the source is CSV or JSON. These distinctions prevent the common mistake of getting a character list when words were intended, or executing input that should only have been data.