DriversRecommendedOutdated drivers can make a good PC feel brokenScan driver issues before chasing fixes manually.Scan NowOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan Now×
Skip to content
World desk3 min

Convert a String to a List in Python Without Using split()

Use list() for a character list, ast.literal_eval() for Python literal text, and json.loads() for JSON arrays. The right conversion depends on the string’s format.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

The right way to convert a string to a Python list without split() depends on what the string represents. Use list(text) when you want one list item per character; use ast.literal_eval() for text written as a Python list literal, or json.loads() for JSON. These methods produce different results, so choose based on the input’s format and intended items.

Make a list of characters

A Python string is a sequence of characters. Passing it to list(), or iterating over it in a comprehension, creates one element for each character—including spaces and punctuation.

text = "red blue"
items = list(text)
# ['r', 'e', 'd', ' ', 'b', 'l', 'u', 'e']

items = [character for character in text]
# ['r', 'e', 'd', ' ', 'b', 'l', 'u', 'e']

Use a comprehension when you also need to transform or filter characters. For example, this keeps characters that str.isalpha() considers alphabetic:

text = "a1b2"
letters = [character for character in text if character.isalpha()]
# ['a', 'b']

This is character filtering, not word splitting. It can discard punctuation or spacing that matters to your data.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Convert text containing a Python list literal

If the string contains Python literal syntax, such as "['red', 'blue']", parse that representation with ast.literal_eval() rather than turning the string’s characters into a list.

import ast

text = "['red', 'blue']"
items = ast.literal_eval(text)
# ['red', 'blue']

ast.literal_eval() handles Python literals and containers, including lists, strings, numbers, dictionaries, tuples, sets, booleans, None, and Ellipsis. It does not evaluate arbitrary expressions, operators, or indexing. A valid input may therefore produce a value that is not a list; check the result’s type if the rest of your program requires a list. Malformed input can raise an exception. See the Python documentation for ast.literal_eval().

Unlike eval(), this function does not execute general Python code or perform name lookups. That does not make it suitable for every untrusted input: Python’s documentation warns that hostile or unusually complex data can consume excessive memory, stack, or CPU, and does not recommend calling it on untrusted data. For adversarial input, use a defined serialization format and suitable size and complexity limits.

Convert a JSON array string

When the text comes from a JSON source, use the JSON decoder. A valid JSON array becomes a Python list:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
import json

text = '["red", "blue"]'
items = json.loads(text)
# ['red', 'blue']

JSON and Python literal syntax are different formats, even when simple examples look similar. Match the decoder to the format used by the producer; invalid JSON raises a decoding error. See the Python JSON documentation.

What if you want words or delimiter-separated fields?

If the intended items are words or fields, identify the delimiter and the input grammar before writing a parser. A character-by-character loop can implement custom parsing, but delimiters inside quotes, escaped delimiters, nested values, and empty fields can make an ad hoc parser error-prone. For structured data, use its standard decoder, such as json.loads() for JSON, or a parser designed for the specific format. Stripping brackets and punctuation is not a general way to recover list items.

Quick decision guide

What the string represents Use What you get
Characters in ordinary text list(text) or a comprehension over text One element per character, including whitespace and punctuation
A Python literal representation ast.literal_eval(text) The represented Python literal; check that its type is the list your code expects
A JSON array json.loads(text) A Python list for a valid JSON array
Words or custom-delimited fields A parser matching the delimiter and grammar Depends on the format; account for quoting, escapes, and empty fields
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Common mistake: expecting words from list()

list("cat") returns ['c', 'a', 't'], not ['cat']. Likewise, putting text that looks like a list inside a string does not create a list automatically. If the string represents serialized items, parse it according to its actual format.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.