Re: Adding to dictionaries

Aaron Watters (aaron@funcity.njit.edu)
Fri, 27 Jan 1995 14:14:10 GMT

I know I'm being a pain in the ass, but I can't help it, sorry

In article <9501260128.AA22698@viper.cs.Virginia.EDU> rust@virginia.edu writes:
>There is no + operator built in for mapping types. I would guess
>because of the problem, what do you do when one of the elements in Y
>is already in X. There are a few options, including clobbering the
>key already in one of them, or making a list.

my kjbuckets C extension module has kjDict addition, EG:

>>> kjDict([(1,2)]) + kjDict([(3,4),(9,10)])
kjDict([(1, 2), (3, 4), (9, 10)])

In case of key collisions the left dict is preferred (maybe it'd
be better to raise an IndexError, dunno). You can test for problems
via:
for k in ( kjSet( D1.keys() ) & ( kjSet( D2.keys() ) ) ).items():
if D1[k] != D2[k]:
raise IndexError, "ambiguous dictionary union"
D3 = D1 + D2
[Okay, so it's a bit convoluted, but it's pretty fast.]

As I said, I couldn't help myself, sorry. -a.
===
``?Co'mo como? !Como como como!''
[``How do I eat? I eat as I eat!'' in Spanish.]