How to Convert String to List in Python?

2 weeks ago 13
Club.noww.in

Recently, someone working with machine learning asked me about converting a string to a list in Python using the split() method. I explained to them how to do this with an example. Also, there are other methods for converting strings to lists. I will explain everything here with examples.

To convert a string to a list in Python using the split() method, you simply call the split() function on the string, specifying the delimiter. For example, address = "123 Main Street, Springfield, IL"; address_list = address.split(", ") will split the string at each comma and space, resulting in ['123 Main Street', 'Springfield', 'IL'].

Convert String to List in Python

Python provides different methods to convert a string to a list. Let me explain each method with examples.

1. Using the split() Method

The split() method is one of the simplest ways to convert a string to a list. It splits the string into a list based on a specified separator.

Syntax:

str.split(sep=None, maxsplit=-1)
  • sep: The delimiter string. If not specified, any whitespace string is a separator.
  • maxsplit: The maximum number of splits. If not specified, there is no limit.

Example:

Here is an example.

address = "123 Main Street, Springfield, IL" address_list = address.split(", ") print(address_list)

This will output:

['123 Main Street', 'Springfield', 'IL']

You can see the exact output in the screenshot below:

string to list python

I highly recommend using this method to convert a string to a list in Python.

Check out Find the First Number in a String in Python

2. Using the list() Function

Let me show you another method. You can also use the list() method to convert a string to a list.

The list() function can be used to convert a string into a list of characters.

Syntax:

list(string)

Example:

Let me show you an example.

city = "Chicago" city_list = list(city) print(city_list)

This will output:

['C', 'h', 'i', 'c', 'a', 'g', 'o']

The exact output is in the screenshot below. I executed the above Python code using VS code.

python string to list

Read Reverse a String in Python

3. Using List Comprehension

List comprehension provides a concise way to create lists. You can use it to convert a string to a list of characters or to apply a condition.

Example:

state = "California" state_list = [char for char in state] print(state_list)

This will output:

['C', 'a', 'l', 'i', 'f', 'o', 'r', 'n', 'i', 'a']

You can see the output in the screenshot below:

convert string to list python

4. Using re.findall()

For more complex string splitting in Python, you can use the re module’s findall() function. This is particularly useful for extracting patterns.

Example:

Here is an example.

import re phone_number = "Call me at 123-456-7890 or 987-654-3210" numbers = re.findall(r'\d{3}-\d{3}-\d{4}', phone_number) print(numbers)

This will output:

['123-456-7890', '987-654-3210']

Check out Check if a Python String Contains a Substring

5. Using ast.literal_eval()

If you have a string representation of a list, you can use ast.literal_eval() to convert it back to a list.

Example:

Below is an example.

import ast list_str = "['New York', 'Los Angeles', 'Chicago']" cities_list = ast.literal_eval(list_str) print(cities_list)

This will output:

['New York', 'Los Angeles', 'Chicago']

Conclusion

In this tutorial, I explained different methods for converting a string to a list in Python, such as the split() method, list() function, list comprehension, re.findall(), or ast.literal_eval(), etc. For each method, I have shown one real example.

You may also like:

Read Entire Article