[Tutor] how do I create a lists of values associated with a key?
Alan Gauld
alan.gauld at btinternet.com
Fri Aug 1 12:25:53 CEST 2008
"Angela Yang" <angelayian at yahoo.com> wrote
> But for dictionaries, it is key - value pairs. But I need key ->
> multiple values.
But a value can be a list.
d = {}
d['odd'] = [1,3,5,7]
d['even'] = [2,4,6,8]
print d['odd'][2] # = 5
See the Raw Materials top[ic in my tutorial for another
example using a dictionary to store address data:
>>> addressBook = {
... 'Fred' : ['Fred', '9 Some St',' Anytown', '0123456789'],
... 'Rose' : ['Rose', '11 Nother St', 'SomePlace', '0987654321']
... }>>> print addressBook['Rose']
['Rose', '11 Nother St', 'SomePlace', '0987654321']
>>> print addressBook['Fred'][3]
0123456789
HTH,
--
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld
More information about the Tutor
mailing list