Python | Convert given list into nested list
Some times, we come over the 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 nested list is quite common in web development. Let’s discuss certain ways in which this can be performed.
Method #1: Using iteration
[[‘Geeeks’, ‘Forgeeks’], [‘65.7492’, ‘62.5405’], [‘Geeks’, ‘123’], [‘555.7492’, ‘152.5406’]]
Method #2 : Using ast [list with numeric values]
[[12, 454], [15.72, 82.85], [52.236, 25256], [95.9492, 72.906]]
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.
Result: [['Geeeks', 'Forgeeks'], ['65.7492', '62.5405'], ['Geeks', '123'], ['555.7492', '152.5406']]
Time complexity: O(n)
Auxiliary Space: O(n)