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 a 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 to filter or transform items. 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 and produces one element for each Unicode code point. It does not split a sentence into words. For example, a combining accent may be a separate code point even when it appears visually attached to a letter.

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

This is useful for character-level processing. Spaces and punctuation are included as characters, so remove or transform them explicitly if the task calls for that.

Split words or delimited values with split()

Call split() without an argument to separate words on runs of whitespace. Leading and trailing whitespace are ignored, and repeated spaces, tabs, or line breaks act as one separator.

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

When the input uses a known delimiter, pass it explicitly. If spaces around comma-separated fields are allowed, trim each field after splitting:

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

An explicit separator preserves empty fields: "red,,blue".split(",") includes an empty string between the commas. If the separator is absent, the original string is returned as the single item. Use maxsplit to limit how many times splitting occurs; "red,green,blue".split(",", maxsplit=1) keeps green,blue together in the second item.

split() is not a CSV parser. When fields may contain quoted delimiters, such as "red, bright",blue, use Python’s csv module so quoting rules are handled correctly.

Parse text that already represents a list

If text already uses Python literal syntax, such as "[1, 2, 3]", parse it with ast.literal_eval(). It accepts literal values and containers without evaluating arbitrary expressions. The text must still be syntactically valid; malformed input can raise ValueError or SyntaxError.

import ast

text = "[1, 2, 3]"
values = ast.literal_eval(text)
if isinstance(values, list):
    print(values)
else:
    print("The parsed value is not a list")

A successful parse does not guarantee a list: for example, "(1, 2)" represents a tuple. Check the parsed type before using it as a list, and handle parse errors where input can vary. For JSON text, use json.loads() because JSON and Python literal syntax are similar but not identical. Treat input as data; never substitute eval() for ast.literal_eval() when the text is not fully controlled.

Transform or filter items with a comprehension

A comprehension can apply a condition or transformation while building the result. This example keeps only digit characters:

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

You can also transform retained characters, such as with [char.lower() for char in text if char.isalpha()]. A comprehension iterates over the string’s characters. If the intended items are words, call split() first and then apply the comprehension to that word list.

Choose the right method

  • Use list(text) when each character should be an item.
  • Use text.split() for whitespace-separated words, or text.split(separator) for simple, consistently delimited fields.
  • Use ast.literal_eval() only when the input is intended to be Python literal data; check the resulting type and handle malformed text.
  • Use json.loads() for JSON and Python’s csv module for quoted CSV fields.
  • Use a comprehension when items also need filtering or transformation.

Decide what the input format guarantees before converting it. Validate the resulting type and required fields at the boundary where external text enters your program, and report malformed data in a way the calling code can handle.