Python - Convert list of string to list of list
Last Updated :
27 Dec, 2024
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.
Python
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 the split()
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.
Python
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 applies split(",")
) and an iterable (the list). - Each string in the
data
list is processed by the split()
method, producing a list of substrings. - The
list()
function converts the result of map()
into a list.
Using a for
Loop
Using a traditional for loop in Python is a simple way to achieve the conversion.
Python
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
result
is initialized to store the transformed data. - The
for
loop iterates over each string (item
) in the list. - The
split(",")
method is applied to each string, and the resulting list is appended to the result
list.
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.
Python
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 pattern r"\|"
matches the pipe (|
) character. - List comprehension is used to apply
re.split()
to each string in the data
list. - The result is a list of lists where each string is split based on the specified pattern.
Similar Reads
Convert string to a list in Python Our task is to Convert string to a list in Python. Whether we need to break a string into characters or words, there are multiple efficient methods to achieve this. In this article, we'll explore these conversion techniques with simple examples. The most common way to convert a string into a list is
2 min read
Python - Converting list string to dictionary Converting a list string to a dictionary in Python involves mapping elements from the list to key-value pairs. A common approach is pairing consecutive elements, where one element becomes the key and the next becomes the value. This results in a dictionary where each pair is represented as a key-val
3 min read
Convert Object to String in Python Python provides built-in type conversion functions to easily transform one data type into another. This article explores the process of converting objects into strings which is a basic aspect of Python programming.Since every element in Python is an object, we can use the built-in str() and repr() m
2 min read
Convert Set to String in Python Converting a set to a string in Python means changing a group of unique items into a text format that can be easily read and used. Since sets do not have a fixed order, the output may look different each time. For example, a set {1, 2, 3} can be turned into the string "{1, 2, 3}" or into "{3, 1, 2}"
2 min read
Convert a List of Characters into a String - Python Our task is to convert a list of characters into a single string. For example, if the input is ['H', 'e', 'l', 'l', 'o'], the output should be "Hello".Using join() We can convert a list of characters into a string using join() method, this method concatenates the list elements (which should be strin
2 min read