Multilevel dicts/arrays v. tuples as keys?

Peter Otten __peter__ at web.de
Mon Nov 14 05:47:58 EST 2011


Matej Cepl wrote:

> Dne 11.11.2011 14:31, macm napsal(a):
>> def Dicty( dict[k1][k2] ):
> 
> When looking at this I returned to the question which currently rolls in
> my mind:
> 
> What's difference/advantage-disadvantage betweeng doing multi-level
> dicts/arrays like this and using tuple as a key? I.e., is it more
> Pythonic to have
> 
> dict[k1,k2]
> 
> instead?

If you need lookup only I'd prefer tuples, but sometimes you may want to 
retrieve all values with a certain k1 and

d[k1]

is certainly more efficient than

[(k2, v) for (k1, k2), v in d.items() if k1 == wanted]





More information about the Python-list mailing list