In this program, we will try to convert a given string to a list, where spaces or any other special characters, according to the user’s choice, are encountered. To do this we use the split() method in string.
Examples:
Input : "Geeks for Geeks"
Output : ['Geeks', 'for', 'Geeks']
Explaination:Here we have a string in the input which we converted into list of words.
String to List Conversion in Python
Below are the methods that we will cover in this article:
- Using list()
- Using list comprehension
- Using split() method
- Using string slicing
- Using re.findall() method
- Using enumerate function
- Using JSON
- Using ast.literal
Python String to List of Characters using list() method
The list is the built-in datatype in Python. it is generally used to store the item or the collection of items in it and we can use it to convert the string to a list.
Python3
s = "Geeks for" x = list (s) print (x) |
Output:
['G', 'e', 'e', 'k', 's', ' ', 'f', 'o', 'r']
Python String to List of Characters using List Comprehension
Here we can also use list comprehension in which we iterate over the string and store it in the list
Python3
s = "Geeks" x = [i for i in s] print (x) |
['G', 'e', 'e', 'k', 's']
Python Convert String to List using split() method
The split method is used to split the strings and store them in the list. The built-in method returns a list of the words in the string, using the “delimiter” as the delimiter string. If a delimiter is not specified or is None, a different splitting algorithm is applied: runs of consecutive whitespace are regarded as a single separator, and the result will contain no empty strings at the start or end if the string has leading or trailing whitespace.
Example 1
Python3
# Python code to convert string to list def Convert(string): li = list (string.split( " " )) return li # Driver code str1 = "Geeks for Geeks" print (Convert(str1)) |
['Geeks', 'for', 'Geeks']
Example 2
Python3
def Convert(string): li = list (string.split( "-" )) return li # Driver code str1 = "Geeks-for-Geeks" print (Convert(str1)) |
Output
['Geeks', 'for', 'Geeks']
Python Convert String to List using String Slicing
In Python, we have Slicing with which we can slice any iterable data according to our needs and use it as required
Python3
def Convert(string): list1 = [] list1[: 0 ] = string return list1 # Driver code str1 = "ABCD" print (Convert(str1)) |
['A', 'B', 'C', 'D']
Python Convert String to List using re.findall() method
This task can be performed using regular expression. We can use the pattern to match all the alphabet and make a list with all the matched elements.
Python3
import re # Function which uses re.findall method to convert string to list character wise def Convert(string): return re.findall( '[a-zA-Z]' , string) # Driver code str1 = "ABCD" print ( "List of character is : " ,Convert(str1)) |
List of character is : ['A', 'B', 'C', 'D']
Python Convert String to List using enumerate function
Python has an inbuilt method enumerate which can use to convert a string to a list
Python3
s = "geeks" x = [i for a,i in enumerate (s) ] print (x) |
['g', 'e', 'e', 'k', 's']
Python Convert String to List using JSON
The json
module in Python provides functions for working with JSON data. It also has loads method which can
Python3
import json stringA = '["geeks", 2,"for", 4, "geeks",3]' # Type check res = json.loads(stringA) # Result print ( "The converted list : \n" ,res) |
The converted list : ['geeks', 2, 'for', 4, 'geeks', 3]
Python Convert String to List using ast.literal
In Python, we have ast module which has a method of litera_eval through which we can also do the conversion
Python3
import ast # initializing string representation of a list ini_list = '["geeks", 2,"for", 4, "geeks",3]' # Converting string to list res = ast.literal_eval(ini_list) # printing final result and its type print (res) print ( type (res)) |
['geeks', 2, 'for', 4, 'geeks', 3] <class 'list'>