Skip to content

How to Convert String to List in Python

Learn Python methods to split strings into lists by words, delimiters, characters, or lines, including split(), list(), splitlines(), and ast.literal_eval() for safe parsing of list representations.

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)

messy_text = "Hello    World\tPython"
print(messy_text.split())

Split String by Custom Delimiter

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

csv_line = "Alice,Admin,active"
fields = csv_line.split(",")

print(fields)

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

print(parts)

log_entry = "ERROR:Database:Connection failed:timeout"
parts = log_entry.split(":", maxsplit=2)

print(parts)

Handle Empty Values in Delimited Data

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

result = data.split(",")
print(result)

filtered = [item for item in data.split(",") if item]
print(filtered)

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)

emoji_text = "Hi! 👋"
print(list(emoji_text))

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)

windows_text = "First\r\nSecond\r\nThird"
print(windows_text.splitlines())

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]"

real_list = ast.literal_eval(list_string)

print(real_list)

print(type(real_list))

print(real_list[0] + real_list[1])

Handle Nested Structures

import ast

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

data = ast.literal_eval(nested_string)

print(data)

print(data[2]['key'])

Split Using Regular Expressions

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

import re

text = "apple;banana,cherry orange"

items = re.split(r'[;,\s]+', text)

print(items)

text = "one1two2three3four"
parts = re.split(r'(\d)', text)

print(parts)

Create List from String with Transformation

Combine splitting with list comprehensions to transform elements during conversion.

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

print(numbers)

print(sum(numbers))

padded_data = " apple , banana , cherry "
clean_items = [item.strip() for item in padded_data.split(",")]

print(clean_items)

Quick Reference

Goal Method Example
Split by whitespace s.split() "a b c" → ['a', 'b', 'c']
Split by delimiter s.split(',') "a,b,c" → ['a', 'b', 'c']
Split into characters list(s) "abc" → ['a', 'b', 'c']
Split by lines s.splitlines() "a\nb" → ['a', 'b']
Parse list string ast.literal_eval(s) "[1,2]" → [1, 2]
Split by pattern re.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.

Navigation