Converting a string into a list in Python
Categories:
Mastering String to List Conversions in Python

Learn various Python techniques to efficiently convert strings into lists, handling different delimiters, data types, and complex scenarios.
Converting a string into a list is a common operation in Python programming, essential for parsing data, processing user input, or manipulating text. Python offers several flexible and powerful methods to achieve this, depending on the structure of your string and the desired output list. This article will guide you through the most common and effective techniques, from simple splitting to more advanced parsing.
Basic String Splitting with split()
The most straightforward way to convert a string into a list is by using the built-in split()
method. This method divides a string into a list of substrings based on a specified delimiter. If no delimiter is provided, split()
defaults to splitting by any whitespace and discards empty strings.
# Splitting by default whitespace
text = "apple banana cherry"
words = text.split()
print(words)
# Output: ['apple', 'banana', 'cherry']
# Splitting by a specific delimiter (comma)
data_string = "red,green,blue"
colors = data_string.split(',')
print(colors)
# Output: ['red', 'green', 'blue']
# Splitting with a limit
long_string = "one two three four five"
limited_split = long_string.split(' ', 2)
print(limited_split)
# Output: ['one', 'two', 'three four five']
Examples of using the split()
method with and without a delimiter, and with a maxsplit
argument.
split()
is called without any arguments, it handles multiple spaces between words gracefully, treating them as a single delimiter. If you specify a space ' '
as the delimiter, it will create empty strings in the list for multiple consecutive spaces.Converting String Characters to a List
Sometimes, you might need to convert each character of a string into an individual element in a list. Python's list()
constructor can directly achieve this, treating the string as an iterable of characters.
my_string = "Python"
char_list = list(my_string)
print(char_list)
# Output: ['P', 'y', 't', 'h', 'o', 'n']
sentence = "Hello World"
char_list_sentence = list(sentence)
print(char_list_sentence)
# Output: ['H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd']
Using list()
to convert a string into a list of its individual characters.
Advanced Parsing with List Comprehensions and map()
For more complex scenarios, such as converting string elements to a different data type (e.g., integers or floats), or applying a transformation during the conversion, list comprehensions and the map()
function are invaluable.
flowchart TD A["Input String (e.g., '1,2,3,4')"] B{"Split by delimiter ','"} C["List of String Elements (e.g., ['1', '2', '3', '4'])"] D{"Convert each element to int (map or comprehension)"} E["List of Integer Elements (e.g., [1, 2, 3, 4])"] A --> B B --> C C --> D D --> E
Flowchart illustrating the process of splitting a string and converting its elements to integers.
# Converting string of numbers to a list of integers using list comprehension
num_string = "10 20 30 40"
int_list_comp = [int(num) for num in num_string.split()]
print(int_list_comp)
# Output: [10, 20, 30, 40]
# Converting string of numbers to a list of floats using map()
float_string = "1.1,2.2,3.3"
float_list_map = list(map(float, float_string.split(',')))
print(float_list_map)
# Output: [1.1, 2.2, 3.3]
# Filtering and converting with list comprehension
mixed_string = "apple,1,banana,2,cherry,3"
numbers_only = [int(item) for item in mixed_string.split(',') if item.isdigit()]
print(numbers_only)
# Output: [1, 2, 3]
Examples demonstrating list comprehensions and map()
for advanced string to list conversions, including type casting and filtering.
int()
or float()
), ensure that the string elements are valid representations of those types. Attempting to convert a non-numeric string to an int
or float
will raise a ValueError
.Using re.split()
for Regular Expression Splitting
For strings with multiple or complex delimiters, Python's re
module (regular expressions) provides the re.split()
function. This is particularly useful when delimiters are not fixed characters but patterns.
import re
# Splitting by multiple delimiters (comma, semicolon, or space)
complex_string = "apple,banana;cherry date"
items = re.split(r'[,;\s]+', complex_string)
print(items)
# Output: ['apple', 'banana', 'cherry', 'date']
# Splitting and keeping the delimiters
text_with_delimiters = "word1-word2_word3"
parts_and_delimiters = re.split(r'([-|_])', text_with_delimiters)
print(parts_and_delimiters)
# Output: ['word1', '-', 'word2', '_', 'word3']
Examples of using re.split()
for splitting strings based on regular expression patterns, including capturing delimiters.
re.split()
function is powerful for handling irregular delimiters. The r
prefix before the string denotes a raw string, which is recommended for regular expressions to avoid issues with backslashes.