Dictionary of Dictionaries

Marc 'BlackJack' Rintsch bj_666 at gmx.net
Mon Mar 5 05:45:38 EST 2007


In <1173090144.783533.163920 at p10g2000cwp.googlegroups.com>, bg_ie wrote:

> 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



More information about the Python-list mailing list