Dictionary attributes and functions

j vickroy jim.vickroy at noaa.gov
Fri Mar 9 23:11:43 CET 2001

Hello David,

See if the following code fragment meets you needs.

mapping = {} # create an empty dictionary

for item in keys_list: # some list of keys you provide -- duplicates ok

  if not mapping.has_key (item):
    mapping [item] = [] # create empty list mapped to key item
  mapping [item] .append (some_value)

iddwb wrote:
> 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
> {keya: [123]}
> on insert of keya againg have something like
> {keya: [123, 234]}
> Any pointers on what to look for in the docs?
> btw, I've been going through Mark Lutz's book on "programming python" but
> I'd like something with a little more langugage reference in it.
> David Bear
> College of Public Programs/ASU

More information about the Python-list mailing list