Python
programming
string manipulation
data conversion
tutorial

How to convert comma-delimited string to list in Python?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

The simplest way to convert a comma-delimited string to a list in Python is split(","). That solves the basic case immediately, but real input often also needs whitespace cleanup, empty-field handling, or proper CSV parsing when commas can appear inside quoted values.

Basic Split

For plain input, use the string split method:

python
1text = "apple,banana,cherry"
2items = text.split(",")
3
4print(items)
text
['apple', 'banana', 'cherry']

This is enough when the input format is controlled and commas are the only separators.

Remove Extra Whitespace

User-entered or copied text often contains spaces after commas. split preserves those spaces, so strip each item if you want clean values.

python
1text = "apple, banana, cherry"
2items = [part.strip() for part in text.split(",")]
3
4print(items)
text
['apple', 'banana', 'cherry']

That list comprehension is the most common improvement over the basic version.

Ignore Empty Values When Needed

Some inputs contain repeated commas or trailing commas:

python
1text = "apple,,banana,cherry,"
2items = [part.strip() for part in text.split(",") if part.strip()]
3
4print(items)
text
['apple', 'banana', 'cherry']

Whether you should drop empty strings depends on the meaning of the data. In some formats, an empty field is significant and must be preserved.

Convert the Elements to Other Types

After splitting, you can convert the list items to integers or other types. For example:

python
1text = "10,20,30"
2numbers = [int(part) for part in text.split(",")]
3
4print(numbers)
text
[10, 20, 30]

This works only if every field is valid for the target type. If the data comes from users or external systems, wrap conversion in validation rather than assuming every field is well-formed.

Use the csv Module for Real CSV Data

If values can contain commas inside quotes, split(",") is the wrong tool. Real CSV parsing should use Python's csv module.

python
1import csv
2
3text = 'apple,"banana, yellow",cherry'
4items = next(csv.reader([text]))
5
6print(items)
text
['apple', 'banana, yellow', 'cherry']

This is the main boundary to remember:

  • 'split(",") is for simple delimited text'
  • 'csv.reader is for actual CSV format'

Confusing the two is a common source of subtle bugs.

Decide on the Input Contract

Before writing the conversion, decide which of these rules apply:

  • should whitespace be trimmed
  • should empty items be preserved
  • can quoted commas appear
  • should values be converted to numbers or kept as strings

The implementation is short, but the data contract matters. Most mistakes come from unclear assumptions about the input rather than from Python syntax.

Turn the Pattern Into a Helper

If the same conversion appears in multiple places, hide the trimming and empty-item policy behind a helper function. That keeps the calling code short and makes it easier to change the parsing rule later.

python
1def parse_tags(text):
2    return [part.strip() for part in text.split(",") if part.strip()]
3
4
5print(parse_tags("python, data,  numpy,"))

Centralizing the logic also prevents different parts of the codebase from interpreting the same input format in slightly different ways.

Common Pitfalls

  • Using split(",") on real CSV data that may contain quoted commas.
  • Forgetting to strip whitespace and ending up with values like " banana".
  • Dropping empty fields even though they carry meaning in the data format.
  • Converting directly to int without validating the input first.
  • Treating all comma-delimited text as equivalent when simple delimiters and CSV have different parsing rules.

Summary

  • 'text.split(",") is the standard solution for simple comma-delimited strings.'
  • Add strip() when input may contain spaces around values.
  • Filter empties only if the format allows missing fields to be ignored.
  • Convert the resulting strings to other types only after deciding how to handle invalid input.
  • Use the csv module when the data follows real CSV rules.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.