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

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python code to convert list 
# of string into list of list
  
# 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 = [] 
  
# Using Iteration to convert 
# element into list of lsit
for elem in temp:
    temp3 = []
    for elem2 in elem:
        temp3.append(elem2)
    Output.append(temp3)
  
# printing
print(Output)

chevron_right


Output:

[[‘Geeeks’, ‘Forgeeks’], [‘65.7492’, ‘62.5405’], [‘Geeks’, ‘123’], [‘555.7492’, ‘152.5406’]]

 
Method #2 : Using ast [list with numeric values]

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

[[12, 454], [15.72, 82.85], [52.236, 25256], [95.9492, 72.906]]



My Personal Notes arrow_drop_up


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : mayank5326



Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.