7 Powerful ways to Convert string to list in Python

Hello coders!! In this article, we will be learning how to convert string to list in python. At first, we must understand the difference between the two. A string in Python can consist of only characters, whereas a list can consist of any data type. So, let us explore the 7 different ways to achieve this conversion.

Method 1: Convert String to list of strings in Python

Syntax:

string.split(separator, maxsplit)

Parameters:

  • Separator: separator to use when splitting the string
    • Default value: whitespace
  • maxsplit: number of splits required
str1 = "Python pool for python knowledge"
list1 = list(str1.split(" ")) 
print(list1) 

Output:

['Python', 'pool', 'for', 'python', 'knowledge']

The split method by default takes whitespace as delimiter and separates the words of the string from by the whitespace and converts them into a list.

Method 2: Convert String to list of characters in Python

To convert a string into list of characters, we can simply use type conversion using the inbuilt list() method.

Syntax:

list(iterable)

Parameter:

  • iterable: an object that could be a sequence/collection/any iterator object
str1 = "Python pool for python knowledge"
list1 = list(str1)
print(list1) 

Output:

['P', 'y', 't', 'h', 'o', 'n', ' ', 'p', 'o', 'o', 'l', ' ', 'f', 'o', 'r', ' ', 'p', 'y', 't', 'h', 'o', 'n', ' ', 'k', 'n', 'o', 'w', 'l', 'e', 'd', 'g', 'e']

Using type conversion with the help of list() method, it directly converts the given string into a list of characters for us.

Method 3: Convert List of Strings to List of Lists using map in Python

str1 = "Python pool for python knowledge"
str1=str1.split()
list1=list(map(list,str1))
print(list1)

Output:

[['P', 'y', 't', 'h', 'o', 'n'], ['p', 'o', 'o', 'l'], ['f', 'o', 'r'], ['p', 'y', 't', 'h', 'o', 'n'], ['k', 'n', 'o', 'w', 'l', 'e', 'd', 'g', 'e']]

In this example, we have used both the split() method and the list() method to obtain the desired result. We first used the split() method to convert the string into a list of strings. We then applied the list() method to an individual element of the list to obtain the list of lists.

Method 4: Convert string consisting of Integers to List of integers in Python:

str1="9 8 7 6 5 4 3 2 1"
list1=list(str1.split())
list2=list(map(int,list1))
print(list2)

Output:

[9, 8, 7, 6, 5, 4, 3, 2, 1]

In this code, we have first used the split() method to first convert the string into list. We then used type casting to convert the individual elements of the list into integer to get the list of integers as output.

Method 5: String to list in Python using custom separator:

str1 = "Python-pool-for-python-knowledge"
list1 = list(str1.split("-")) 
print(list1) 

Output:

['Python', 'pool', 'for', 'python', 'knowledge']

In this example, we have used a ‘ – ‘ to split our string and convert it into a list of strings. As the default value of split() is whitespace and in this example, we have used a custom separator ‘ – ‘ to split our string.

Method 6: Parsing string to list using JSON:

import json
json_str = '{"str": ["Python", "pool", "for", "python", "knowledge"]}'
json_obj = json.loads(json_str)
list1 = json_obj["str"]
print(list1)

Output:

['Python', 'pool', 'for', 'python', 'knowledge']

At first, we have used json.loads() method to convert the JSON string into the dictionary.  We then used the index of the dictionary to return the list stored at the index key.

Method 7: Parsing string to list using ast:

import ast 

str1 = "['Python', 'pool','for','python', 'knowledge']"
print (type(str1)) 
print(str1)
list1 = ast.literal_eval(str1) 
print (type(list1)) 
print (list1)

Output:

< class 'str' >
[ 'Python' ,  'pool' , 'for' , 'python' ,  'knowledge' ]
< class 'list' >
[ 'Python' ,  'pool' ,  'for' ,  'python' ,  'knowledge' ]

The ast.literal_eval() is used to evaluate a python expression.

Conclusion: Convert string to list python

With this, we come to the end of the article. These are the different ways of converting a string into a list. One can use any of the above methods as per their convenience and requirement.

However, if you have any doubts or questions, do let me know in the comment section below. I will try to help you as soon as possible.

Happy Pythoning!

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments