Add two dicts

Afanasiy abelikov72 at hotmail.com
Fri Aug 29 13:42:18 EDT 2003


On Fri, 29 Aug 2003 08:40:01 GMT, Alex Martelli <aleax at aleax.it> wrote:

>Afanasiy wrote:
>
>> On Thu, 28 Aug 2003 22:07:06 -0700, Erik Max Francis <max at alcyone.com>
>> wrote:
>> 
>>>Afanasiy wrote:
>>>
>>>> Can I add two dicts in a way which is not cumbersome to the above %
>>>> string
>>>> operation? Is this another case of writing my own function, or does a
>>>> builtin (or similar) already exist for this?
>>>
>>>combinedDict = aDict.copy()
>>>combinedDict.update(anotherDict)
>>>
>>>If that's cumbersome, don't really know what you'd consider
>>>non-cumbersome.
>> 
>> Don't really know if you're asking, but :
>> 
>>   vars(self)+{'x':'123','y':'345'}
>> 
>> I would consider that non-cumbersome. ;-)
>

>[snipped alex code]
>
>and now, dad(vars(self), x='123', y='345') -- ain't that even
>LESS cumbersome?  You can't do that with the syntax of + (no
>keyword arguments)

Since you asked...

To me, this clouds the issue and developers using dad() may not actually
be aware they are adding dictionaries. Any function you write is fine for
your own use of course. I would just simply prefer {}+{}.

I know I can write a function to add them and call that. Thank you.




More information about the Python-list mailing list