Python | Convert list of string to list of list
Many times, we come over the dumped data that is found in the string format and we require it to be represented into the actual list format in which it was actually found. This kind of problem of converting a list represented in string format back to list to perform tasks is quite common in web development. Let’s discuss certain ways in which this can be performed.
Method #1 : Using strip() + split()
A combination of strip and split function can perform a particular task. The strip function can be used to get rid of the brackets and split function can make the data list comma-separated.
# Python3 code to demonstrate
# to convert list of string to list of list
# using strip() + split()
# initializing list
test_list = [ '[1, 4, 5]', '[4, 6, 8]' ]
# printing original list
print ("The original list is : " + str(test_list))
# using strip() + split()
# to convert list of string to list of list
res = [i.strip("[]").split(", ") for i in test_list]
# printing result
print ("The list after conversion is : " + str(res))
|
Output :
The original list is : ['[1, 4, 5]', '[4, 6, 8]'] The list after conversion is : [['1', ' 4', ' 5'], ['4', ' 6', ' 8']]Method #2 : Using list slicing and split()
The task performed in the above method can also be performed using the list slicing in which we slice all the elements from second to second last element hence omitting the last brackets.
# Python3 code to demonstrate
# to convert the list of string to list of list
# using list slicing + split()
# initializing list
test_list = [ '[1, 4, 5]', '[4, 6, 8]' ]
# printing original list
print ("The original list is : " + str(test_list))
# using list slicing + split()
# to convert list of string to list of list
res = [i[1 : -1].split(', ') for i in test_list]
# printing result
print ("The list after conversion is : " + str(res))
|
Output :
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course
Python | Program to convert String to a List
In this program, we will try to convert a given string to a list, where spaces or any other special characters, according to the users choice, are encountered. To do this we use the split() method.
Syntax:
Examples:
Input : "Geeks for Geeks" Output : ['Geeks', 'for', 'Geeks'] Input : "Geeks-for-Geeks" Output : ['Geeks', 'for', 'Geeks']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:
# 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))
|
Output:
['Geeks', 'for', 'Geeks']Example 2:
# 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))
|
Output:
['Geeks', 'for', 'Geeks']Example 3:
# Python code to convert string to list character-wise
def Convert(string):
list1=[]
list1[:0]=string
return list1
# Driver code
str1="ABCD"
print(Convert(str1))
|
Output:
['A','B','C','D']Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course
Introduction
While programming we may need to convert a string to list in Python. That could be for any other reason. But, a question arises here, how can we convert a string to different forms of lists?
So, here in this tutorial, we are going to learn how we can convert a string into a list in Python.
Convert string to list in Python
In this short tutorial, find how to convert string to list in Python. We look at all the ways you can achieve this along with their pros and cons.Convert list of string to list of list in Python
In this article we will see how to create a list of lists which contain string data types. The inner list themselves or of string data type and they may contain numeric or strings as their elements.
Python: Convert a given list of strings into list of lists using map function
Last update on December 29 2020 12:44:33 (UTC/GMT +8 hours)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
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
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.