Skip to content

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

Learn Python's best methods to convert a comma-separated string into a clean list, handle whitespace, filter empty values, and process numeric or quoted CSV data efficiently.

Converting a comma-separated string into a list is one of the most common string-processing tasks in Python. You'll encounter this when parsing CSV data, handling user input, reading configuration values, processing query parameters, or splitting tags and categories.

In this guide, you'll learn multiple methods to convert a comma-delimited string to a list in Python, handle edge cases like whitespace and empty values, and choose the right approach for your use case.

The split() method is the most straightforward and efficient way to convert a comma-delimited string to a list. It divides the string at each occurrence of the specified delimiter and returns a list of substrings.

s = "apple,banana,cherry,date"

result = s.split(',')
print(result)

Output:

['apple', 'banana', 'cherry', 'date']

This works cleanly when the string has no extra spaces around the commas.

Handling Whitespace with split() and strip()

Real-world data often includes spaces after commas. The basic split(',') preserves those spaces:

s = "apple, banana, cherry, date"

result = s.split(',')
print(result)

Output:

['apple', ' banana', ' cherry', ' date']

Notice the leading spaces on ' banana', ' cherry', and ' date'. To remove them, combine split() with strip() using a list comprehension:

s = "apple, banana, cherry, date"

result = [item.strip() for item in s.split(',')]
print(result)

Output:

['apple', 'banana', 'cherry', 'date']

Using split() Without Arguments for Flexible Splitting

When called without arguments, split() splits on any whitespace (spaces, tabs, newlines) and automatically handles multiple consecutive spaces. However, this doesn't work for comma-delimited strings unless you replace commas first:

s = "apple, banana, cherry, date"

result = s.replace(',', ' ').split()
print(result)

Output:

['apple', 'banana', 'cherry', 'date']

Using re.split() for Complex Delimiters

When the delimiter pattern is more complex, such as commas optionally followed by spaces, or multiple delimiter types, use re.split() from the re module:

import re

s = "apple, banana,  cherry,date,  elderberry"

result = re.split(r',\s*', s)
print(result)

Output:

['apple', 'banana', 'cherry', 'date', 'elderberry']

Splitting on Multiple Delimiters

import re

s = "apple,banana;cherry|date grape"

result = re.split(r'[,;|\s]+', s)
print(result)

Output:

['apple', 'banana', 'cherry', 'date', 'grape']

Converting to Specific Data Types

Often the comma-separated values represent numbers, not just strings. Use map() or list comprehension to convert types during splitting:

Converting to Integers

s = "10, 20, 30, 40, 50"

result = [int(x.strip()) for x in s.split(',')]
print(result)
print(type(result[0]))

Output:

[10, 20, 30, 40, 50]
<class 'int'>

Using map() for Type Conversion

s = "1.5, 2.7, 3.14, 4.0"

result = list(map(float, s.split(',')))
print(result)

Output:

[1.5, 2.7, 3.14, 4.0]

Handling Edge Cases

Empty Strings Between Commas

When there are consecutive commas, split(',') produces empty strings:

s = "apple,,banana,,,cherry"

result = s.split(',')
print(result)

Output:

['apple', '', 'banana', '', '', 'cherry']

To filter out empty values:

s = "apple,,banana,,,cherry"

result = [item for item in s.split(',') if item]
print(result)

Output:

['apple', 'banana', 'cherry']

Empty Input String

s = ""

result = s.split(',')
print(result)
print(len(result))

Output:

['']
1

Single Value (No Commas)

s = "apple"

result = s.split(',')
print(result)

Output:

['apple']

This correctly returns a single-element list.

Using the csv Module for Quoted Values

When comma-separated values contain commas within quoted strings, the simple split(',') approach breaks down. Use Python's csv module for proper parsing:

import csv
import io

s = 'Alice,"Smith, Jr.",30,"New York, NY"'

reader = csv.reader(io.StringIO(s))
result = next(reader)
print(result)

Output:

['Alice', 'Smith, Jr.', '30', 'New York, NY']

The csv module correctly handles quoted fields containing commas, which split(',') cannot do:

print(s.split(','))

Practical Example: Parsing Configuration Values

config_line = "allowed_hosts = localhost, 127.0.0.1, example.com, *.example.org"

key, value = config_line.split('=', 1)

hosts = [host.strip() for host in value.split(',')]

print(f"Key: {key.strip()}")
print(f"Hosts: {hosts}")

Output:

Key: allowed_hosts
Hosts: ['localhost', '127.0.0.1', 'example.com', '*.example.org']

Quick Comparison of Methods

Method Handles Spaces Handles Quotes Type Conversion Best For
s.split(',') ❌ ❌ ❌ Clean, simple strings
split(',') + strip() ✅ ❌ ❌ Most real-world data
re.split(r',\s*', s) ✅ ❌ ❌ Complex/multiple delimiters
map(int, s.split(',')) ❌ ❌ ✅ Numeric strings (no spaces)
List comprehension ✅ ❌ ✅ Full control, filtering
csv.reader() ✅ ✅ ❌ Quoted CSV fields

Conclusion

Converting a comma-delimited string to a list in Python is straightforward, but choosing the right method depends on your data:

  • Use split(',') for clean strings without extra whitespace. It's the simplest and fastest option.
  • Use split(',') with strip() in a list comprehension for real-world data that may contain inconsistent spacing. This is the most commonly needed approach.
  • Use re.split() when dealing with complex or multiple delimiters.
  • Use map() or list comprehension with type conversion when values represent numbers.
  • Use the csv module when values may contain commas within quoted fields.

For most everyday use cases, [item.strip() for item in s.split(',')] provides the best combination of simplicity and reliability.

Navigation