Python - Convert list of string to list of list
In Python, we often encounter scenarios where we might have a list of strings where each string represents a series of comma-separated values, and we want to break these strings into smaller, more manageable lists. In this article, we will explore multiple methods to achieve this.
Using List Comprehension
List comprehension is a concise way to create lists. By combining it with the split() method, which splits a string into a list based on a specified delimiter, we can efficiently transform a list of strings into a list of lists.
a = ["GeeksforGeeks"]
result = [item.split(",") for item in a]
print(result)
Output
[['GeeksforGeeks']]
Explanation:
- The
split(",")method is applied to each string in the list, splitting it wherever a comma appears. - The list comprehension iterates over each string (
item) in the list and applies thesplit()method. - The result is a new list, where each string has been converted into a list of substrings.
Let's explore some more methods and see how we can convert a list of strings to a list of lists.
Using map() with split()
map() function applies a specified function to each item in an iterable. When combined with the split() method, it can be used to transform a list of strings into a list of lists.
a = ["Learn,Python,with,Gfg", "GeeksforGeeks"]
res = list(map(lambda x: x.split(","), a))
print(res)
Output
[['Learn', 'Python', 'with', 'Gfg'], ['GeeksforGeeks']]
Explanation:
- The
map()function takes two arguments: a function (in this case, a lambda function that appliessplit(",")) and an iterable (the list). - Each string in the
datalist is processed by thesplit()method, producing a list of substrings. - The
list()function converts the result ofmap()into a list.
Using a for Loop
Using a traditional for loop in Python is a simple way to achieve the conversion.
a = ["Learn,Python,with,GFG", "GeeksforGeeks"]
res = []
for item in a:
res.append(item.split(","))
print(res)
Output
[['Learn', 'Python', 'with', 'GFG'], ['GeeksforGeeks']]
Explanation:
- An empty list
resultis initialized to store the transformed data. - The
forloop iterates over each string (item) in the list. - The
split(",")method is applied to each string, and the resulting list is appended to theresultlist.
Using Regular Expressions with re.split()
If the delimiter is more complex (e.g., multiple delimiters or patterns), regular expressions provide a powerful alternative. The re.split() function can split strings based on patterns rather than fixed delimiters.
import re
a = ["Learn|Python|with|GFG", "Geeks|for|Geeks"]
res = [re.split(r"\|", item) for item in a]
print(res)
Output
[['Learn', 'Python', 'with', 'GFG'], ['Geeks', 'for', 'Geeks']]
Explanation:
- The
re.split()function splits strings based on a regular expression pattern. Here, the patternr"\|"matches the pipe (|) character. - List comprehension is used to apply
re.split()to each string in thedatalist. - The result is a list of lists where each string is split based on the specified pattern.