extending dictonary

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Sat Nov 21 04:50:28 EST 2009


On Sat, 21 Nov 2009 09:25:38 +0100, nospam wrote:

> Is there any way to extend the dictonary in such manner that I can
> insert muliplay value to each keys and return one of the value as the
> default value. I would like to have similar syste that I drawed out
> below.
> 
> 
> tree[nucelotide_postionc][nucleotide]=default(value subtree) This should
> be returned when doing lookup without any
> tree[nucelotide_postionc][nucleotide]=Postive value
> tree[nucelotide_postionc][nucleotide]=Negative value

I'm sorry, I don't understand what you are asking. What are tree, 
nucelotide_postionc, nucleotide, subtree? What are positive value and 
negative value?

If each key has multiple values, how do you want to specify which value 
to return?


The simplest way to associate multiple values with a key is to use a list:

>>> d = {}
>>> d['a'] = [2, 3, 4]
>>> d['b'] = [5, 6]
>>> d['a'].append(0)
>>> d
{'a': [2, 3, 4, 0], 'b': [5, 6]}

If you like, you can take the convention that the first item in the list 
is the default, and write this:

>>> print d['b'][0]
5


-- 
Steven



More information about the Python-list mailing list