How convert list to nested dictionary?

Boris Borcic bborcic at gmail.com
Fri Nov 5 14:42:59 CET 2010


Arnaud Delobelle wrote:
> macm<moura.mario at gmail.com>  writes:
>
>> Hi Folks
>>
>> How convert list to nested dictionary?
>>
>>>>> l
>> ['k1', 'k2', 'k3', 'k4', 'k5']
>>>>> result
>> {'k1': {'k2': {'k3': {'k4': {'k5': {}}}}}}
>>
>> Regards
>>
>> macm
>
> reduce(lambda x,y: {y:x}, reversed(l), {})
>

d={}
while L : d={L.pop():d}





More information about the Python-list mailing list