Sometimes, we come across data that is in string format in a list and it is required to convert it into a list of the list. This kind of problem of converting a list of strings to a nested list is quite common in web development. Let’s discuss certain ways in which this can be performed.
Method #1: Using iteration
Python3
# Python code to convert list # of string into list of list # List initialization Input = [ 'Geeeks, ForLazyroar' , '65.7492, 62.5405' , 'Geeks, 123' , '555.7492, 152.5406' ] temp = [] # Getting elem in list of list format for elem in Input : temp2 = elem.split( ', ' ) temp.append((temp2)) # List initialization Output = [] # Using Iteration to convert # element into list of list for elem in temp: temp3 = [] for elem2 in elem: temp3.append(elem2) Output.append(temp3) # printing print (Output) |
[[‘Geeeks’, ‘ForLazyroar’], [‘65.7492’, ‘62.5405’], [‘Geeks’, ‘123’], [‘555.7492’, ‘152.5406’]]
Method #2: Using list with numeric values
Python3
# Python code to convert list # of string into list of list # importing import ast # List Initialization Input = [ '12, 454' , '15.72, 82.85' , '52.236, 25256' , '95.9492, 72.906' ] # using ast to convert Output = [ list (ast.literal_eval(x)) for x in Input ] # printing print (Output) |
[[12, 454], [15.72, 82.85], [52.236, 25256], [95.9492, 72.906]]
Time complexity: O(n), where n is the length of the input list Input.
Auxiliary space: O(n), where n is the length of the input list Input.
Method #3: Using map()+split()+lambda
We use the map function to apply a function (in this case, the split function) to each element in a list (in this case, the list of strings). The map function returns an iterator that applies the function to each element of the list, and returns the results as a new iterator.
To obtain the final result as a list, we use the list function to convert the iterator returned by map into a list.
Python3
#initializing list of strings Input = [ 'Geeeks, ForLazyroar' , '65.7492, 62.5405' , 'Geeks, 123' , '555.7492, 152.5406' ] #use the map function to apply the split function to each string in the list Output = list ( map ( lambda x: x.split( ', ' ), Input )) print ( "Result: " + str (Output)) # Output: [['Geeeks', 'ForLazyroar'], ['65.7492', '62.5405'], ['Geeks', '123'], ['555.7492', '152.5406']] #This code is contributed by Edula Vinay Kumar Reddy |
Result: [['Geeeks', 'ForLazyroar'], ['65.7492', '62.5405'], ['Geeks', '123'], ['555.7492', '152.5406']]
Time complexity: O(n)
Auxiliary Space: O(n)
Method 4: Using list comprehension.
The program converts a list of strings into a list of lists by splitting each string by a comma followed by a space. Here’s the idea of the program in three lines:
Here are the steps:
- Initialize an empty list called output.
- Use a list comprehension to split each string in Input by the comma followed by a space (‘, ‘), and convert the result into a list.
- Append the resulting list to output.
- Print output.
Example:
Python3
# Python code to convert list of string into list of list # List initialization Input = [ 'Geeeks, ForLazyroar' , '65.7492, 62.5405' , 'Geeks, 123' , '555.7492, 152.5406' ] # Using list comprehension to convert # element into list of list output = [elem.split( ', ' ) for elem in Input ] # printing print (output) |
[['Geeeks', 'ForLazyroar'], ['65.7492', '62.5405'], ['Geeks', '123'], ['555.7492', '152.5406']]
Time complexity: O(n), where n is the number of strings in Input.
Auxiliary Space: O(n), because the resulting list output has n sublists, each containing two elements.
Method #5: Using numpy library
- Import the numpy library using the following code:
- Initialize the input list with string values separated by commas and spaces using the following code:
- Use a list comprehension to split each element in the input list using commas and spaces as delimiters, and create a list of lists with the split values. This is done using the following code:
- Convert the list of lists into a 2D numpy array using the following code:
- Convert the 2D numpy array back to a list of lists using the tolist() method, and assign the result to the variable output using the following code.
- Print the variable output using the following code:
Python3
import numpy as np Input = [ 'Geeeks, ForLazyroar' , '65.7492, 62.5405' , 'Geeks, 123' , '555.7492, 152.5406' ] output = np.array([elem.split( ', ' ) for elem in Input ]) print (output.tolist()) |
Output:
[['Geeeks', 'ForLazyroar'], ['65.7492', '62.5405'], ['Geeks', '123'], ['555.7492', '152.5406']]
Time complexity: O(n)
Auxiliary Space: O(n)
Method #6: Using heapq:
- Initialize the input array with the following values
- Create an empty output list.
- For each element elem in Input, do the following:
- Split elem into a list of strings using the comma and space characters as delimiters.
- Push the resulting list onto the output list using heapq.heappush().
- Create a new empty list called result.
- While the output list is not empty, do the following:
- Pop the smallest element off the output list using heapq.heappop().
- Append the popped element to the result list.
- Print the resulting result list.
Python3
import heapq Input = [ 'Geeeks, ForLazyroar' , '65.7492, 62.5405' , 'Geeks, 123' , '555.7492, 152.5406' ] output = [] for elem in Input : heapq.heappush(output, elem.split( ', ' )) output = [heapq.heappop(output) for i in range ( len (output))] print (output) #This code is contributed by Rayudu. |
[['555.7492', '152.5406'], ['65.7492', '62.5405'], ['Geeeks', 'ForLazyroar'], ['Geeks', '123']]
Time complexity:
The time complexity of the code is O(n log n), where n is the number of elements in the Input list. This is because the code performs two loops:
In the first loop, it takes each element in the Input list, splits it into a list, and adds it to a heap using the heapq.heappush() function. The heapq.heappush() function takes log(n) time, so this loop has a time complexity of O(n log n).
In the second loop, it pops each element from the heap using the heapq.heappop() function and adds it to a list. The heapq.heappop() function also takes log(n) time, so this loop also has a time complexity of O(n log n).
The total time complexity of both loops combined is O(n log n).
Auxiliary Space:
The space complexity of the code is O(n), where n is the number of elements in the Input list. This is because the code creates a new list to hold the split elements from Input, and another list to hold the sorted elements. Additionally, the heapq module itself creates a heap that has a maximum size of n. Therefore, the overall space complexity is O(n).