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 DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix Now×
Skip to content
World desk2 min

How to Convert a Comma-Separated String to a List in Python

Split simple comma-separated text with Python’s split(','); trim item boundaries when needed, or use csv.reader for quoted CSV fields and embedded commas.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

For simple text with commas between values, call split(','): text.split(',') returns a Python list. Trim each result with strip() if values may have spaces around them. If the input follows CSV quoting rules or values can contain commas, use Python’s standard-library csv reader instead.

Split a simple comma-separated string

Use the string method split(',') when commas are literal separators and fields do not need quoting:

text = "apple,banana,cherry"
items = text.split(',')
print(items)
# ['apple', 'banana', 'cherry']

The explicit comma tells Python where to divide the string. The result is a list of strings; it does not convert values to numbers or other types. See the Python 3.14.8 documentation for str.split.

Trim spaces around each value

If spaces around the values are incidental, strip each part after splitting:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
text = "apple, banana ,cherry"
items = [part.strip() for part in text.split(',')]
# ['apple', 'banana', 'cherry']

strip() removes whitespace at the beginning and end of each value. It leaves whitespace inside a value untouched. Do not use text.split() for comma-separated input: without an explicit separator, split() separates on whitespace instead.

Decide what empty fields mean

With an explicit comma separator, Python splits at every comma and preserves empty fields as empty strings. For example, '1,,2'.split(',') returns ['1', '', '2']; a trailing comma produces a final empty string, and ''.split(',') returns ['']. This behavior can preserve missing positions in your data. If empty fields should instead be discarded, filter them deliberately:

items = [part.strip() for part in text.split(',') if part.strip()]

That filter removes both empty fields and values containing only whitespace. Keep it out if those fields carry meaning. The Python documentation describes how explicit separators handle consecutive delimiters in its str.split reference.

Use a CSV reader when fields can contain commas

split(',') does not interpret quotation marks. Given one,"two, three",four, it also splits at the comma inside the quoted value, so it cannot correctly parse that CSV row. For CSV-formatted text, use the standard-library csv module:

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

text = 'one,"two, three",four'
items = next(csv.reader([text]))
# ['one', 'two, three', 'four']

The example wraps the text in a one-element iterable because csv.reader reads records from an iterable of lines. For files with multiple rows, pass the file object to csv.reader and process its records rather than treating the whole file as one string. Consult the Python 3.14.8 csv reading and writing documentation for the reader’s behavior and usage.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Choose the right method

Input Use Why
Plain values separated by literal commas text.split(',') Directly splits at each comma.
Plain values with incidental surrounding spaces [part.strip() for part in text.split(',')] Splits at commas, then trims each value’s boundaries.
CSV fields with quotes or embedded commas csv.reader Parses CSV quoting instead of splitting every comma.

Both approaches use Python functionality: split is a built-in string method, and csv is part of the standard library.

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.

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
Outdated Drivers Are Slowing You DownFree scan - exact matches
Windows Errors? Fix Them Before They SpreadFree repair scan

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.