0

I want to create this string into nested list or dictionary.how can i do that?my code is wrong can u correct it.

l="1,john, London,2,zoe,new York" m=[] for i in range(2): m.append([]) for i in range(2): for j in range(3): m[i].append(l.split(",")) print(m) this code is for nested list but it doesn't work. ex:{{regno:1, name:john,place: London},{regno:2,name: Zoe,place:new York}} and a nested list: ex:[[regno:1, name:john,place: London],[regno:2,name: Zoe,place:new York]]

14th Dec 2018, 2:16 PM
Mara
4 Answers
+ 5
If I've understood the question properly then this is something what you're searching for: https://code.sololearn.com/cpqv1AX6lOLg/?ref=app I've just used one of many data structures to format the data. Other data structures can be used as well.
14th Dec 2018, 3:24 PM
ŠØŠ°Ń‰Šø Š Š°Š½Š¶Š°Š½
ŠØŠ°Ń‰Šø Š Š°Š½Š¶Š°Š½ - avatar
+ 3
I would recommend using a namedtuple for this kind of thing: https://code.sololearn.com/c3UuNeCvD1d0/?ref=app
14th Dec 2018, 6:38 PM
Anna
Anna - avatar
+ 1
l="1,john, London,2,zoe,new York" m=[] for i in range(2): m.append([]) print(m) for i in range(2): for j in range(3): m[i].append(l.split(",")) print(m) I removed the ; after the m in the first print statement and it ran.
14th Dec 2018, 2:21 PM
Chris Ford
Chris Ford - avatar
0
But it still does not work
14th Dec 2018, 2:24 PM
Mara