git.net

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Multidimensional dicts


Recently I wrote a quick and dirty script to do some counting and 
statistics. When I re-read it a bit later I noticed that I had been 
using two different ways to create two-dimensional (default-)dicts. Now 
I'm wondering whether one of them is "better" or more pythonic than the 
other.

What I did:

ddd_a = collections.defaultdict(set)
ddd_a[(key1, key2)].add(foo)

ddd_b = collections.defaultdict(lambda: collections.defaultdict(set))
ddd_b[key1][key2].add(foo)

Both work as expected.

Trying to think about differences I only noticed that ddd_a more easily 
generalises to more dimensions, and ddd_b has the benefit that 
ddd_b[key1] is a dict, which might help if one "row" needs to be fed to 
a function that expects a dict.

More general ddd_a looks more symmetric (key1 and key2 are exchangeable, 
if done consistently) and ddd_b looks more hierarchic (like a tree 
traversed from root to leaves where key1, key2 etc. determine which way 
to go at each level). ddd_b also is more simmilar to how two-dimensional 
lists are done in python.

Any recommendations / comments as to which to prefer?