dictionary object

Christian Tismer tismer at appliedbiometrics.com
Mon May 10 15:02:21 EDT 1999


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?

You would like to change a tuple but cannot change a tuple.
So how about a list? :-)

dict = { "pluto" : [10, value1], "pippo" : [20, value2] }

dict["pluto"][-1] = "what was the question?"

ciao - chris

-- 
Christian Tismer             :^)   <mailto:tismer at appliedbiometrics.com>
Applied Biometrics GmbH      :     Have a break! Take a ride on Python's
Kaiserin-Augusta-Allee 101   :    *Starship* http://starship.python.net
10553 Berlin                 :     PGP key -> http://wwwkeys.pgp.net
PGP Fingerprint       E182 71C7 1A9D 66E9 9D15  D3CC D4D7 93E2 1FAE F6DF
     we're tired of banana software - shipped green, ripens at home




More information about the Python-list mailing list