Adding to an Array inside a Dict

MacOSX @ Rocteur.cc macosx at rocteur.cc
Mon Oct 11 10:55:25 EDT 2010


Hi,

Is there an easier way to do this, I am adding/creating to an array inside a
dict but can find a cleaner way:

So I first check if the key exists already, if it does then I know I use
append, if it does not I create it.

        if osmdict.has_key(token):
            osmdict[token]['user'].append(user)
            osmdict[token]['reason'].append(reason)
        else:
            osmdict[token] = { 'user' : [user], 'reason' : [reason]}

Sorry if I've completely overlooked the obvious,

Thanks in advance,

Jerry
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20101011/d3ec1086/attachment.html>


More information about the Python-list mailing list