Preferred method for "Assignment by value"

duncan smith buzzard at urubu.freeserve.co.uk
Tue Apr 15 21:07:14 CEST 2008


hall.jeff at gmail.com wrote:
> Thank you both, the assigning using slicing works perfectly (as I'm
> sure you knew it would)... It just didn't occur to me because it
> seemed a little nonintuitive... The specific application was
> 
> def dicttolist (inputdict):
>     finallist=[]
>     for k, v in inputdict.iteritems():
>         temp = v
>         temp.insert(0,k)
>         finallist.append(temp)
> 
>     return finallist
>

Maybe,

finallist = [[k] + v for k, v in inputdict.iteritems()]

the list concatenation creating new lists.

Duncan



More information about the Python-list mailing list