Convert String to List in Python

Here are the ten ways to convert a string to a list in Python:

  1. Using split()
  2. Using list()
  3. Using strip() and split()
  4. Using string slicing
  5. Using re.findall() method
  6. Using list comprehension
  7. Using enumerate() function
  8. Using json.loads()
  9. Using ast.literal()
  10. Using the filter() with lambda function

Method 1: Using split()

The split() method divides a string into substrings by using a specified ‘delimiter’ and returns a list of these substrings.

Visual Representation

Method 1 - Using string split()

Example

new_str = "I love Python"
print ("Initial string : ", new_str) 
print (type(new_str))

new_list = list(new_str.split(" "))

print("=== After Converted ===") 

print ("New list : ", new_list) 
print (type(new_list))

Output

Initial string : I love Python
<class 'str'>
=== After Converted ===
New list : ['I', 'love', 'Python']
<class 'list'>

Method 2: Using list()

If you want to convert a string into a list of its individual characters, you can do so simply by using list conversion.

Example

new_str = "KDL"
print ("Initial string : ", new_str) 
print (type(new_str))

new_list = list(new_str)

print("=== After Converted ===") 

print ("New list : ", new_list) 
print (type(new_list))

Output

Initial string : KDL
<class 'str'>
=== After Converted ===
New list : ['K', 'D', 'L']
<class 'list'>

Method 3: Using strip() and split() methods

You can remove the square brackets from the string using the strip() method and then divide the string at each occurrence of a comma followed by a space using the split() method. Finally, print the resulting list.

Visual Representation

Method 2 - Using strip() and split() methods

Example

new_str = "[1, 2, 3, 4, 5]"
 
print ("Initial string", new_str) 
print (type(new_str))
 
new_list = new_str.strip('][').split(', ')

print("=== After Converted ===")

print ("New list", new_list)
print (type(new_list))

Output

Initial string [1, 2, 3, 4, 5]
<class 'str'>
=== After Converted ===
New list ['1', '2', '3', '4', '5']
<class 'list'>

Method 4: Using string slicing

The string slicing splits the string into its constituent characters and adds them to a list.

Visual Representation

Method 3 - Using string slicing

Example

new_str = "KDL"

print ("initial string : ", new_str) 
print (type(new_str))

new_list = []
new_list[:0] = new_str

print("=== After Converted ===") 

print ("new list : ", new_list) 
print (type(new_list))

Output

initial string : KDL
<class 'str'>
=== After Converted ===
new list : ['K', 'D', 'L']
<class 'list'>

Method 5: Using the re.findall() method

The re.findall() method is used to search for all occurrences of a specified regular expression pattern in a string and returns them as a list.

Visual Representation

Method 4 - Using the re.findall() method

Example

import re

new_str = "KDL"
print ("Initial string : ", new_str) 
print (type(new_str))

new_list = re.findall('[a-zA-Z]', new_str)

print("=== After Converted ===") 

print ("New list : ", new_list) 
print (type(new_list))

Output

Initial string : KDL
<class 'str'>
=== After Converted ===
New list : ['K', 'D', 'L']
<class 'list'>

Method 6: Using list comprehension

List comprehension can be used to iterate over each character in a string, creating a list of these characters.

Visual Representation

Method 5 - Using list comprehension

Example

new_str = "KDL"
print ("Initial string : ", new_str) 
print (type(new_str))

new_list = [i for i in new_str]

print("=== After Converted ===") 

print ("New list : ", new_list) 
print (type(new_list))

Output

Initial string : KDL
<class 'str'>
=== After Converted ===
New list : ['K', 'D', 'L']
<class 'list'>

Method 7: Using the enumerate() function

Visual Representation

Method 6 - Using the enumerate() functionExample

new_str = "KDL"
print ("Initial string : ", new_str) 
print (type(new_str))

new_list = [i for a,i in enumerate(new_str) ]

print("=== After Converted ===") 

print ("New list : ", new_list) 
print (type(new_list))

Output

Initial string : KDL
<class 'str'>
=== After Converted ===
New list : ['K', 'D', 'L']
<class 'list'>

Method 8: Using json.loads()

This method is specifically used when the string is in JSON format.

Visual Representation

Method 7 - Using json.loads()Example

import json

new_str = "[1, 2, 3, 4, 5]"
print ("Initial string : ", new_str) 
print (type(new_str))

new_list = json.loads(new_str)

print("=== After Converted ===") 

print ("New list : ", new_list) 
print (type(new_list))

Output

Initial string : [1, 2, 3, 4, 5]
<class 'str'>
=== After Converted ===
New list : [1, 2, 3, 4, 5]
<class 'list'>

Method 9: Using ast.literal() method

The ast.literal_eval method is a safe way to evaluate a string containing a literal or container display.

Visual Representation

Method 8 - Using ast.literal() methodExample

import ast

new_str = "[1, 2, 3, 4, 5]"
print ("Initial string : ", new_str) 
print (type(new_str))

new_list = ast.literal_eval(new_str)

print("=== After Converted ===") 

print ("New list : ", new_list) 
print (type(new_list))

Output

Initial string : [1, 2, 3, 4, 5]
<class 'str'>
=== After Converted ===
New list : [1, 2, 3, 4, 5]
<class 'list'>

Method 10: Using the filter() with lambda function

The filter() function applies the lambda function to each character in the string and includes the character in the new list if the lambda function returns True.

Visual Representation

Method 9 - Using the lambda functionExample

new_str = "KDL"
print ("Initial string : ", new_str) 
print (type(new_str))

new_list = list(filter(lambda i:(i in new_str), new_str))

print("=== After Converted ===") 

print ("New list : ", new_list) 
print (type(new_list))

Output

Initial string : KDL
<class 'str'>
=== After Converted ===
New list : ['K', 'D', 'L']
<class 'list'>

Related Posts

Python string to int

Python string to float

Python string to json

Python string to datetime

Python string to dict

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.