dictionary object

William Park parkw at better.net
Mon May 10 15:38:34 EDT 1999


On Mon, May 10, 1999 at 06:03:48PM +0000, Diego Dainese wrote:
> Hi,
> 
> I have a question regarding the dictionary objects: in a dictionary
> where the values are tuples, is it possible to get the tuple with a
> given key and to change the value of one element of this tuple using
> only one key lookup?
> 
> In example, if dict is defined like this:
> 
>    value1 = "hello one"
>    value2 = "hello two"
>    dict = { "pluto" : (10, value1), "pippo" : (20, value2) }
> 
> And I want to get the first element of the tuple with the key "pluto"
> and change the second element of this same tuple, I must do something
> like this:
> 
>   num = dict["pluto"][0]
>   dict["pluto"]= (num, "hello three")
> 
> Thus making two key lookups. There is a better way to do it?
> 
> Thanks in advance,
> 
> -- 
> Diego Dainese
> To reply me, remove the numbers and the `x' from my address
> Sorry for my bad English!

Tuple is immutable, so you should use list instead.  For example,
    dict = { "pluto" : [10, value1], "pippo" : [20, value2] }

William Park




More information about the Python-list mailing list