Initialise dictionary of dictionary

Tim Chase python.list at tim.thechases.com
Thu Jan 23 10:30:23 EST 2014


On 2014-01-23 07:15, Ayushi Dalmia wrote:
> I need to initialise a dictionary of dictionary with float values.
> I do not know the size of the dictionary beforehand. How can we do
> that in Python --
 
Either

  d = {}

or, if you want

  from collections import defaultdict
  d = defaultdict(float)
  print(d["Hello"])

If you really do want a dict-of-dict that defaults to floats, you can
do

  d = defaultdict(lambda: defaultdict(float))
  print(d[3141]["Hello"])

-tkc






More information about the Python-list mailing list