How to add a key:datum pair to the dictionary

MK mark_removethis_ at _removethis_btweng.krakow.pl
Mon May 17 04:58:53 EDT 1999


On 16 May 1999 20:51:55 +0200, Hrvoje Niksic <hniksic at srce.hr> wrote:

>dict = {}
>dict['mk'] = {'repetitions': 20, 'no match': 10}

It does work, thanks.

>> I have not found the method for this anywhere in documentation.
>> Dictionary is mutable, but neither Library Reference nor Language
>> Reference give information on how to _add_ something to dictionary.

>This is untrue.  If you read carefully enough, you'll find:

>    *Mappings*
>         These represent finite sets of objects indexed by arbitrary index
>         sets. The subscript notation `a[k]' selects the item indexed by
>         `k' from the mapping `a'; this can be used in expressions and as
>         the target of assignments or `del' statements. 

I read this text, it's just this 'target of assignment' escaped my
attention because it is a little confusing, it is not explicitly said
that new items can be created that way. I mean, in case of list 
you can't do something like adding in dictionary:

a=[5,6]

a[2]=7

I understand now that in case of mapping type it is perfectly OK,
but since I am complete newbie to Python, it did not seem clear.

>But you don't have to search through Python-Ref if you don't want to
>(it's for language lawyers anyway) -- such info is available in the
>tutorial, under the node titled "Dictionaries".

You're right, I did not follow precisely an example code. It is
there in form of adding phone number of Guido to dictionary. :-)





--------------------------------------------------
Reality is something that does not disappear after
you cease believing in it - VALIS, Philip K. Dick
--------------------------------------------------

Delete _removethis_ from address to email me




More information about the Python-list mailing list