Skip to main content

How to Convert String to List in Python

Converting strings to lists is a versatile operation with different approaches depending on your goal, for example, splitting by words, separating characters, parsing delimited data, or evaluating list representations.

Split String into Words with split()​

The split() method without arguments separates a string by whitespace and handles multiple spaces intelligently.

text = "Apple Banana Cherry"

words = text.split()

print(words)
# Output: ['Apple', 'Banana', 'Cherry']

# Handles multiple spaces and tabs
messy_text = "Hello World\tPython"
print(messy_text.split())
# Output: ['Hello', 'World', 'Python']
info

When called without arguments, split() treats consecutive whitespace characters as a single separator and ignores leading and trailing whitespace.

Split String by Custom Delimiter​

Pass a delimiter argument to split() for CSV data, paths, or other structured formats.

# Split CSV data
csv_line = "Alice,Admin,active"
fields = csv_line.split(",")

print(fields)
# Output: ['Alice', 'Admin', 'active']

# Split path components
path = "/home/user/documents"
parts = path.split("/")

print(parts)
# Output: ['', 'home', 'user', 'documents']

# Split with limit (maxsplit parameter)
log_entry = "ERROR:Database:Connection failed:timeout"
parts = log_entry.split(":", maxsplit=2)

print(parts)
# Output: ['ERROR', 'Database', 'Connection failed:timeout']

Handle Empty Values in Delimited Data​

data = "one,,two,,,three"

# split() preserves empty strings
result = data.split(",")
print(result)
# Output: ['one', '', 'two', '', '', 'three']

# Filter empty values if needed
filtered = [item for item in data.split(",") if item]
print(filtered)
# Output: ['one', 'two', 'three']

Convert String to Character List with list()​

The list() constructor converts any iterable to a list. For strings, this produces a list of individual characters.

text = "Python"

characters = list(text)

print(characters)
# Output: ['P', 'y', 't', 'h', 'o', 'n']

# Works with Unicode
emoji_text = "Hi! 👋"
print(list(emoji_text))
# Output: ['H', 'i', '!', ' ', '👋']

Split by Lines with splitlines()​

For multi-line strings, splitlines() handles different line ending conventions.

text = """Line one
Line two
Line three"""

lines = text.splitlines()

print(lines)
# Output: ['Line one', 'Line two', 'Line three']

# Handles Windows line endings too
windows_text = "First\r\nSecond\r\nThird"
print(windows_text.splitlines())
# Output: ['First', 'Second', 'Third']

Parse List String Representation with ast.literal_eval()​

When you have a string that looks like a Python list, use ast.literal_eval() to safely convert it to an actual list object.

import ast

list_string = "[1, 2, 3, 'hello', True]"

# Convert string representation to real list
real_list = ast.literal_eval(list_string)

print(real_list)
# Output: [1, 2, 3, 'hello', True]

print(type(real_list))
# Output: <class 'list'>

print(real_list[0] + real_list[1])
# Output: 3

Handle Nested Structures​

import ast

nested_string = "[[1, 2], [3, 4], {'key': 'value'}]"

data = ast.literal_eval(nested_string)

print(data)
# Output: [[1, 2], [3, 4], {'key': 'value'}]

print(data[2]['key'])
# Output: value
warning

Never use eval() to parse strings from untrusted sources. It executes arbitrary code and creates serious security vulnerabilities. Always use ast.literal_eval(), which only evaluates literal expressions.

Split Using Regular Expressions​

For complex splitting patterns, use re.split() from the re module.

import re

text = "apple;banana,cherry orange"

# Split by multiple delimiters
items = re.split(r'[;,\s]+', text)

print(items)
# Output: ['apple', 'banana', 'cherry', 'orange']

# Split and keep delimiters
text = "one1two2three3four"
parts = re.split(r'(\d)', text)

print(parts)
# Output: ['one', '1', 'two', '2', 'three', '3', 'four']

Create List from String with Transformation​

Combine splitting with list comprehensions to transform elements during conversion.

# Convert CSV numbers to integers
numbers_str = "10,20,30,40,50"
numbers = [int(x) for x in numbers_str.split(",")]

print(numbers)
# Output: [10, 20, 30, 40, 50]

print(sum(numbers))
# Output: 150

# Strip whitespace from each element
padded_data = " apple , banana , cherry "
clean_items = [item.strip() for item in padded_data.split(",")]

print(clean_items)
# Output: ['apple', 'banana', 'cherry']

Quick Reference​

GoalMethodExample
Split by whitespaces.split()"a b c" → ['a', 'b', 'c']
Split by delimiters.split(',')"a,b,c" → ['a', 'b', 'c']
Split into characterslist(s)"abc" → ['a', 'b', 'c']
Split by liness.splitlines()"a\nb" → ['a', 'b']
Parse list stringast.literal_eval(s)"[1,2]" → [1, 2]
Split by patternre.split(pattern, s)Complex delimiters

Conclusion​

Choose your conversion method based on the input format: split() for word or delimiter-separated text, list() for character-level conversion, splitlines() for multi-line content, and ast.literal_eval() for parsing string representations of Python lists. For complex splitting patterns, re.split() provides the flexibility of regular expressions.