3 ms·
Only if you're using a list, not a dict. I just tried it in the REPL: >>> foo = {} >>> foo[3] = 72 >>> foo[64] = 80 >>> foo {3: 72, 64: 80}
by amyjess 9y ago
Only if you're using a list, not a dict. I just tried it in the REPL:
>>> foo = {}
>>> foo[3] = 72
>>> foo[64] = 80
>>> foo
{3: 72, 64: 80}
- arunix 9y agoThe difference is more dramatic once you have two or more dimensions: >>> foo = {} >>> foo[3][4] = 42 Traceback (most recent call last): File "<stdin>", line 1, in <module> KeyError: 3 Compared to: DB<1> $foo{3}{4} = 42 DB<2> x \%foo 0 HASH(0x7f9f4315c940) 3 => HASH(0x7f9f432bb148) 4 => 42
- pseudalopex 9y agotree = lambda: collections.defaultdict(tree) foo = tree() foo[3][4] = 42