How to Convert String to List in Python

Often Python developers need to convert strings into Lists in Python. There are several ways to do this. You may convert the entire string into a list of individual words, or a string into a list of characters. You may also extract certain specific parts of a string and convert it into a list. In this article, we will learn several different ways to convert string to list in Python.

How to Convert String to List in Python

Here are the different ways to convert string to list in Python.

1. Using split()

Split() function in Python allows you to split a string into separate substrings, as per your requirement. Here is its syntax.

string.split(separator)

You can call split() function directly from any string literal or variable. Its default delimiter used for splitting, is whitespace character. You can optionally specify the delimiter if it is something else. You can leave it blank if you want to split based on whitespace character.

Here is an example to illustrate it.

data='good morning'
data2=data.split()
print(data2) # output is ['good', 'morning']

Here is an another example to split a date into its individual components.

data='2025-04-30'
data2=data.split('-')
print(data2) # output is ['2025', '04', '30']

Using split() function is useful if you want to split a sentence into a list of individual words. But if you want to split it into individual characters then you need to use other solutions described below.

2. Using list()

Python also supports built-in function list() which converts a string into a list of characters in Python.

list(string)

Here is an example to use list() function to convert a string into a list of separate characters.

data='good morning'
data2=list(data)
print(data2) # output is ['g', 'o', 'o', 'd', ' ', 'm', 'o', 'r', 'n', 'i', 'n', 'g']

This approach is useful if you want to work with individual characters in a string.

3. Using List Comprehension

List comprehensions provide an efficient and concise way to loop through a string and convert it into a list. Here is an example to convert a string into a list.

data='good morning'
data2=[i for i in data]
print(data2) # output is ['g', 'o', 'o', 'd', ' ', 'm', 'o', 'r', 'n', 'i', 'n', 'g']

This solution provides a great advantage that you can easily control the conversion of string to list. Here is an example to convert each letter to uppercase while converting the string into a list, using upper() function.

data='good morning'
data2=[i.upper() for i in data]
print(data2) # ['G', 'O', 'O', 'D', ' ', 'M', 'O', 'R', 'N', 'I', 'N', 'G']

Here is an example where we exclude character ‘o’ from the conversion and use only the remaining characters in the list.

data='good morning'
data2=[i for i in data if i!='o']
print(data2) # output is ['g', 'd', ' ', 'm', 'r', 'n', 'i', 'n', 'g']

4. Using For loop

This is the good old fashioned way of looping through a string using ‘for’ statement and then adding individual characters to an empty list.

data='good morning'
data2=[]
for i in data:
data2.append(i)
print(data2) # output is ['g', 'o', 'o', 'd', ' ', 'm', 'o', 'r', 'n', 'i', 'n', 'g']

This method is more verbose than the rest but it also gives you superb control over conversion of string to lists. Here is an example to add only alternate characters to the list.

data='good morning'
data2=[]
for i in range(0,len(data),2):
data2.append(data[i])
print(data2) # output is ['g', 'o', ' ', 'o', 'n', 'n']

Here is an example to convert a string into list where each list item is a pair of consecutive characters.

data='good morning'
data2=[]
for i in range(0,len(data),2):
data2.append(data[i]+data[i+1])
print(data2) # ['go', 'od', ' m', 'or', 'ni', 'ng']

As you can see, you can use for loops for complex use cases that cannot be solved by directly calling above-mentioned functions.

5. Using Regular Expressions

Regular expressions are super useful if you want to extract certain special characters or patterns from a string and add them to a list. Here is an example to extract only letters from a string and store them in a list.

import re

data = "good morning1.2.3"
letters = re.findall(r'[a-zA-Z]', data)
print(letters) # Output: ['g', 'o', 'o', 'd', 'm', 'o', 'r', 'n', 'i', 'n', 'g']

In the above code, we first import re Python library to process regular expressions. Then we call findall() function and specify the regular expressions for letters, along with the original string. This returns all the letters as a list.

Here is an example to extract only the digits in a string and store it in a list.

import re

data = "good morning1.2.3"
numbers = re.findall(r'\d', data)
print(numbers) # Output: ['1', '2', '3']

6. Using Chunks

Sometimes, you may need to split a string into chunks and add them to a list. We have seen a similar use case above using for loops. Here we will see how to do this using list comprehensions.

data = "good morning"
chunk_size = 2
chunks = [data[i:i+chunk_size] for i in range(0, len(data), chunk_size)]
print(chunks) # output is ['go', 'od', ' m', 'or', 'ni', 'ng']

In the above code, we define chunk_size=2. Then in our list comprehension, we use a list of indexes generated using range() function. We also provide chunk_size as step increment for range. So we get a list of indexes [0, 2, 4, 6 …]. We iterate from 0 to last index in the list. In each iteration, we use slicing to extract a substring starting from a given index, whose length is equal to chunk_size. We add this substring to the list.

Conclusion

In this article, we have learnt several different ways to convert strings to lists in Python. You can use any of these methods, depending on your requirement. If you want to convert a string into a list of words, use split() function. If you want to convert it into a list of individual characters, use list() function. If you want to apply certain conditions or exclude certain characters during conversion, then you can use list comprehension or for loop. If you want to extract certain specific substrings or patterns and convert them into list, then you can use regular expressions.

Also read:
How to Check if Variable is Defined in Python
How to Reverse String in Python
How to Compare Strings in Python

Leave a Reply

Your email address will not be published. Required fields are marked *