Dictionary of Dictionaries

Amit Khemka khemkaamit at gmail.com
Mon Mar 5 05:45:02 EST 2007


On 5 Mar 2007 02:22:24 -0800, bg_ie at yahoo.com <bg_ie at yahoo.com> wrote:
> Hi,
>
> I have the following -
>
> messagesReceived = dict.fromkeys(("one","two"), {})

This will create a dictionary "messagesReceived", with all the keys
referring to *same instance* of the (empty) dictionary.
(   try: messagesReceived = dict( [(k,{}) for k in ('one', 'two') ] )   )
>
> messagesReceived['one']['123'] = 11111
> messagesReceived['two']['121'] = 22222
> messagesReceived['two']['124'] = 43333
>
> This gives:
>
> {'two': {'121': 22222, '123': 11111, '124': 43333}, 'one': {'121':
> 22222, '123': 11111, '124': 43333}}

And hence the results !


HTH,

-- 
----
Amit Khemka -- onyomo.com
Home Page: www.cse.iitd.ernet.in/~csd00377
Endless the world's turn, endless the sun's Spinning, Endless the quest;
I turn again, back to my own beginning, And here, find rest.



More information about the Python-list mailing list