Open In App
Related Articles

Python | Convert given list into nested list

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

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.

Convert the Given List into Nested List in Python

Below are the ways by which we can convert a given list into a Nested List in Python:

Convert the Given List into Nested List Using Iteration

In this example, the Python code takes a list of strings, where each string contains comma-separated values, and converts it into a list of lists. It achieves this by first splitting each string into individual elements and then organizing them into sublists, resulting in the desired list of lists named “Output”.

Python3

# List initialization
Input = ['Geeeks, Forgeeks', '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 = []
 
for elem in temp:
    temp3 = []
    for elem2 in elem:
        temp3.append(elem2)
    Output.append(temp3)
 
# printing
print(Output)

                    

Output
[['Geeeks', 'Forgeeks'], ['65.7492', '62.5405'], ['Geeks', '123'], ['555.7492', '152.5406']]

Python Convert List into Nested List Using List With Numeric Values

In this example, the Python code utilizes the ast.literal_eval function to safely convert a list of comma-separated string elements (Input) into a list of lists (Output) containing numerical values, demonstrating an approach that ensures proper evaluation and avoids potential security risks.

Python3

# 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)

                    

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. 

Convert Python List into Nested List Using map(), split() and 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, Forgeeks', '65.7492, 62.5405',
         'Geeks, 123', '555.7492, 152.5406']
 
Output = list(map(lambda x: x.split(', '), Input))
print("Result: "+str(Output))

                    

Output
Result: [['Geeeks', 'Forgeeks'], ['65.7492', '62.5405'], ['Geeks', '123'], ['555.7492', '152.5406']]

Time complexity: O(n)
Auxiliary Space: O(n)

Turning a List into Nested Lists in Python 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. In this example, the Python code initializes a list of strings (Input) with comma-separated values. It employs list comprehension to split each string into elements and create a list of lists (output). The result is then printed, showcasing a concise approach for transforming the input strings into organized sublists.

Python3

# List initialization
Input = ['Geeeks, Forgeeks', '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)

                    

Output
[['Geeeks', 'Forgeeks'], ['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.

Python Convert List into Nested List Using numpy

In this example, the Python code leverages NumPy to transform a list of strings (Input) with comma-separated values into a NumPy array (output). The array is created using list comprehension and then converted back to a nested list for printing.

Python3

import numpy as np
 
Input = ['Geeeks, Forgeeks', '65.7492, 62.5405',
         'Geeks, 123', '555.7492, 152.5406']
 
output = np.array([elem.split(', ') for elem in Input])
print(output.tolist())

                    

Output:

[['Geeeks', 'Forgeeks'], ['65.7492', '62.5405'], ['Geeks', '123'], ['555.7492', '152.5406']]

Time complexity: O(n)
Auxiliary Space: O(n)

Python Turning a List into Nested Lists Using heapq

In this example, the Python code utilizes the heapq module to create a min-heap (output) of lists resulting from splitting strings in a list (Input) with comma-separated values. The min-heap is then popped to retrieve the elements in ascending order, demonstrating the use of heaps for ordered processing.

Python3

import heapq
 
Input = ['Geeeks, Forgeeks', '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)

                    

Output
[['555.7492', '152.5406'], ['65.7492', '62.5405'], ['Geeeks', 'Forgeeks'], ['Geeks', '123']]

Time complexity: O(n log n)
Auxiliary Space: O(n)



Last Updated : 07 Dec, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads