Dictionary attributes and functions

Tim Hochberg tim.hochberg at ieee.org
Fri Mar 9 23:12:03 CET 2001

"iddwb" <iddwb at imap1.asu.edu> wrote in message
news:Pine.LNX.4.21.0103092014190.19640-100000 at moroni.pp.asu.edu...
> I'm looking to read a list of items where their may be duplicates.  I'd
> like to use a dictionary to do this but am way to new to python to know
> fully how to do it.  I'm hoping there is some set of fuctions or
> attributes that can be reference regarding dictionaries... specifically if
> key(a) is already in dictionsary x I'd like to know prior to
> adding.  rather than change the value of key(a) I'd like to add the value
> of key(`a) to the value of key(a) so I end up with

For some details of the dictionary methods try

> {keya: [123]}
> on insert of keya againg have something like
> {keya: [123, 234]}

For this particular problem, you probably want the oddly named setdefault
method :

    myDict.setdefault(aKey, []).append(aValue)

This will also work:

if myDict.has_key(aKey):
   myDict[key] = [aValue]


More information about the Python-list mailing list