Case insensitive Dictionary?

Alex cut_me_out at hotmail.com
Thu Sep 21 18:23:30 EDT 2000


Dale:
> Can I do a case-insensitve has_key test or lookup on a dictionary
> without taking a copy of the keys (which would kind of slow things
> down a lot!) ?

Not really.  Your best bet is probably to write a class that keeps the
keys in a case-insensitive form.  E.g.

from UserDict import UserDict

class MyDict(UserDict):

    def __setitem__(self, key, value):
        UserDict.__setitem__(self, key.lower(), value)

    def __getitem__(self, key):
        return UserDict.__getitem__(self, key.lower())

This is untested, and uses Python2.0, but hopefully it gets the general
idea across.

Alex.

-- 
Speak softly but carry a big carrot.



More information about the Python-list mailing list