Converting List to dicts


#1

Can anyone tell me how to convent a list to list of dicts ?


#2

convert? Can you give an example of how you would this to look like?


#3

l = ['a' , 'b' , 'c' ,'d']
required output
dl = [{'a' : 'b', 'c' : 'd'}]


#4

that is surprisingly tricky, took me a second. General idea:

l = ['a' , 'b' , 'c' ,'d']
dl=[]
d={}
counter = 0;
list_length = len(l)
while counter < list_length:
     d[l[counter]] = l[counter+1]
     counter += 2
dl.append(d)
print dl

not the greatest code in the world, but its a start


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.