list -> dict help

Bill Scherer scherbi at bam.com
Mon Jan 31 11:53:41 EST 2000


What's the best way for me to do this:

say I have a list:

>>> l = ['a', 'b', 'c', 1]

and I want to turn it into a dictionary that looks like this:

>>> print d
{'a': {'b': {'c': 1}}}

where the length of list l is >= 3.

Basically, list[:-2] represents the nested keys, list[-2] is a terminal
key, and list[-1] is the value.

It seems I need somthing to recur over list[:-2], then add the terminal
info but I can't figure it out...

Thanks!


--
William K. Scherer
Sr. Member of Applications Staff
Bell Atlantic Mobile







More information about the Python-list mailing list