Dictionary of Dictionaries

B

bg_ie

Hi,

I have the following -

messagesReceived = dict.fromkeys(("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}}

but I expected -

{'one': {'121': 22222, }, 'two': {'123': 11111, '124': 43333}}

What am I doing wrong?

Thanks,

Barry.
 
A

Amit Khemka

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,

--
 
M

Marc 'BlackJack' Rintsch

What am I doing wrong?

`dict.fromkeys()` stores the given object for all keys, so you end up with
the *same* dictionary for 'one' and 'two'.

In [18]: a = dict.fromkeys(("one","two"), {})

In [19]: a
Out[19]: {'two': {}, 'one': {}}

In [20]: a['one']['x'] = 42

In [21]: a
Out[21]: {'two': {'x': 42}, 'one': {'x': 42}}

In [22]: a['one'] is a['two']
Out[22]: True

Ciao,
Marc 'BlackJack' Rintsch
 
B

bg_ie

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.

Thanks for your help!
 
B

Bart Ogryczak

messagesReceived = dict.fromkeys(("one","two"), {})

This creates two references to just *one* instance of empty
dictionary.
I'd do it like:
messagesReceived = dict([(key, {}) for key in ("one","two")])
 
D

Duncan Booth

Bart Ogryczak said:
messagesReceived = dict.fromkeys(("one","two"), {})

This creates two references to just *one* instance of empty
dictionary.
I'd do it like:
messagesReceived = dict([(key, {}) for key in ("one","two")])
Alternatively use a defaultdict (Python 2.5) and you don't need to know the
keys in advance:
from collections import defaultdict
messagesReceived = defaultdict(dict)
messagesReceived['one']['123'] = 11111
messagesReceived['two']['121'] = 22222
messagesReceived['two']['124'] = 43333
messagesReceived
defaultdict(<type 'dict'>, {'two': {'121': 22222, '124': 43333}, 'one':
{'123': 11111}})
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top