[Tutor] how do I create a lists of values associated with a key?
Marc Tompkins
marc.tompkins at gmail.com
Fri Aug 1 12:20:43 CEST 2008
On Thu, Jul 31, 2008 at 8:16 PM, Angela Yang <angelayian at yahoo.com> 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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20080801/c6ce3a88/attachment.htm>
More information about the Tutor
mailing list