Dear Angela,

in order to do this, the setdefault function of the dictionaries is very useful.

For example:
mydict = {}
mylist = mydict.setdefault(mykey, [])
mylist.append(myvalue)

"setdefault" either returns the already existing list or sets a new list for the key and returns it.

Regards,
Benoit Thiell.

On Fri, 1 Aug 2008, Marc Tompkins wrote:



On Thu, Jul 31, 2008 at 8:16 PM, Angela Yang <[EMAIL PROTECTED]> wrote:
      Hi,

      I have a list of values for one key.  How do I specify this data 
structure?

      First tried,

      collection = []
      collection['abby'].append('apprentice1')
      collection['abby'].append('apprentice2')

      That did not work because list index is not numeric.
      But for dictionaries, it is key - value pairs.  But I need key -> 
multiple values.

      Do you have some example how to specify this?

      Angela


Each "value" can, itself, be a container - a tuple, a list, or another 
dictionary.

dictTuple = {"a":(1,2,3), "b":(4,5,6)}
dictList = {"a":[1,2,3], "b":[4,5,6]}
dictDict = {"a":{"c":"1","d":"2","e":"3"}, "b":{"f":"4","g":"5","h":"6"}}

Retrieving values:
valValue = dictTuple["a"][0]  # 1
valValue = dictTuple["b"][2]  # 6

Lists work just the same:
valValue = dictList["a"][0]  # 1
valValue = dictList["b"][2]  # 6

Dictionaries are, well, like dictionaries:
valValue = dictDict["a"]["c"]  # 1
valValue = dictDict["b"]["h"]  # 6


Hope that helps....

--
www.fsrtechnologies.com

_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to