False and 0 in the same dictionary

Paul Rubin http
Thu Nov 6 19:54:42 EST 2008


Prateek <surekap at gmail.com> writes:
> > How about using (x, type(x)) as the key instead of just x?
> Yup. I thought of that. Although it seems kinda unpythonic to do so.
> Especially since the dictionary is basically a cache mostly containing
> strings. Adding all the memory overhead for the extra tuples seems
> like a waste just for those four keys.

You could use a second dict for the other type:

  def lookup(x):
    if x in dict1: return dict1[x]
    return dict2[x]

dict1 would have the 4 special keys and dict2 would have the regular
keys.



More information about the Python-list mailing list