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]]
4 Respostas
+ 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.
+ 3
I would recommend using a namedtuple for this kind of thing:
https://code.sololearn.com/c3UuNeCvD1d0/?ref=app
+ 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.
0
But it still does not work