Preferred method for "Assignment by value"

hall.jeff at gmail.com hall.jeff at gmail.com
Tue Apr 15 20:02:13 CEST 2008


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

to convert a dictionary to a list. We deal with large amounts of
bankdata which the dictionary is perfect for since loan number is a
perfect key... at the end, though, I have to throw it into a csv file
and the csv writer doesn't like dictionaries (since the key is an
iterable string it iterates over each value in the key)

by changing temp = v[:] the code worked perfectly (although changing
temp.insert(0,k) to temp = [k] + temp also worked fine... I didn't
like that as I knew it was a workaround)

Thanks again for the help



More information about the Python-list mailing list