Re: Adding to dictionaries

jtv2j@server.cs.Virginia.EDU
Wed, 25 Jan 1995 20:28:55 -0500

Sorry, I was a bit too hasty in mailing that last reply out, I didn't
read your question carefully enough.

If you want to add 'foo' : 2 to a dictionary X:
X['foo'] = 2

If you have a dictionary X and a dictionary Y, and you want to add Y
to X:

for element in Y.keys():
X[element] = Y[element]

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.

Ie, if you had:
x = {'foo' : 1}
y = {'foo' : 2}

then did x + y would you expect: {'foo' : 2} or {'foo' :[1,2]}??

John

------------------------------------------------------------------------------|
|John Viega "Time is the school in which we learn, |
|rust@virginia.edu Time is the fire in which we burn." |
|http://uvacs.cs.virginia.edu/~jtv2j/ |
|University of Virginia --Delmore Schwartz |
-------------------------------------------------------------------------------