list -> dict help

Remco Gerlich scarblac-spamtrap at pino.selwerd.nl
Mon Jan 31 12:52:47 EST 2000


Remco Gerlich wrote in comp.lang.python:
> def convert(l):
>     if len(l) == 1:
> 	   return l[0]
> 	else:
> 	   return { l[0] : convert(l[1:]) }
> 	   
> But this copies the list every call, and recursion can use a lot of stack.
> Since it's tail recursion, there must be an iterative version.

Actually, this being Python, it's all done by reference and the lists
aren't copied all of the time. My brain is still thinking in terms of
other languages now and then...

That means the recursive solution is quite good already, I think.
Can't benchmark them at the moment though.


Also, my editor treats .py files and Usenet posts differently space/tab-wise, 
and copying the functions from my testfile mangled the indentation :(.


-- 
Remco Gerlich,  scarblac at pino.selwerd.nl
Hi! I'm a .sig virus! Join the fun and copy me into yours!



More information about the Python-list mailing list