python - How to make dictionary from list -


my list:

list = ['name1', 'option1.1 value1.1', 'option1.2 value1.2', 'name2', 'option2.1 value2.1', 'option2.2 value2.2', 'option2.3 value2.3'] 

and want create dictionary this:

dict = {'name1':{'option1.1':'value1.1', 'option1.2':'value1.2'}, 'name2':{'option2.1': 'value2.1', 'option2.2':'value2.2', 'option2.3':'value2.3'} 

i don't know how big list (numbers of names, options , values). idea?

with list , dict comprehension:

id=[b[0] b in enumerate(lst) if 'name' in b[1]]+[none] d={lst[id[i]]:dict(map(str.split,lst[id[i]+1:id[i+1]])) in range(len(id)-1)} 

your original list here named lst

this perhaps not readable answer @sr22222, perhaps it's faster, , matter of taste.


Comments

Popular posts from this blog

delphi - How to convert bitmaps to video? -

jasper reports - Fixed header in Excel using JasperReports -

python - ('The SQL contains 0 parameter markers, but 50 parameters were supplied', 'HY000') or TypeError: 'tuple' object is not callable -