If you really need the syntax in your question, defaultdict is probably the best way to get it:
from collections import defaultdict def rec_dd(): return defaultdict(rec_dd) l = rec_dd() l[3] = 'foo' print l {3: 'foo'} l = rec_dd() l[0][2] = 'xx' l[1][0] = 'yy' print l <long output because of defaultdict, but essentially) {0: {2: 'xx'}, 1: {0: 'yy'}}
This is not exactly a βlist of lists,β but it works more or less like.
You really need to specify a precedent, though ... the above has some advantages (you can access the indices without checking if they exist first), and some disadvantages - for example, l[2] in the usual dict will return KeyError , but in defaultdict it just creates an empty defaultdict , adds it and returns it.
Other possible implementations to support various syntactic sugars may include custom classes, etc., and will have other tradeoffs.
Corley brigman
source share