Here are the ten ways to convert a string to a list in Python:
- Using split()
- Using list()
- Using strip() and split()
- Using string slicing
- Using re.findall() method
- Using list comprehension
- Using enumerate() function
- Using json.loads()
- Using ast.literal()
- 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
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
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
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
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
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
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
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
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
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
Krunal Lathiya is a seasoned Computer Science expert with over eight years in the tech industry. He boasts deep knowledge in Data Science and Machine Learning. Versed in Python, JavaScript, PHP, R, and Golang. Skilled in frameworks like Angular and React and platforms such as Node.js. His expertise spans both front-end and back-end development. His proficiency in the Python language stands as a testament to his versatility and commitment to the craft.