1

Experts,

I am coming from Perl background and very much used to using nested hash structures like $row{dept}{owner}{name}{access} = "\\server\path";

Tried doing similar in Python using,

row = collections.defaultdict(dict)

row[dept][owner] works but cannot do row[dept][owner][name][access]

Tried string concat to merge 4 values into 2 like, row[dept_owner][name_access] but that does not solve the purpose. Say, if I directly want to find keys/values for row["IT"]["John Smith"]["Reporting"]

Should I improve on defaultdict or there is another/better way to do this, really appreciate it.

progmatico
  • 4,714
  • 1
  • 16
  • 27
  • Welcome to StackOverflow! – progmatico May 10 '18 at 16:39
  • You can do this with the existing `defaultdict`. Please see the accepted answer of the linked question for details; the other answers there also contain useful information. – PM 2Ring May 10 '18 at 16:43
  • The problem is that you are creating a `defaultdict` that as values creates regular `dict`. You get the solution in the "This question already has an answer here:" link. Anyway, it seems that you may be using wrong tool for the job. I don't know your case, but seems that you may want to use ORM like PeeWee or just a few well named classes linked between. Getting deep into nested dicts with literal keywords is like asking for trouble :) – Ctrl-C May 10 '18 at 16:49

0 Answers0