Case insensitive Dictionary?

Dale Strickland-Clark dale at out-think.NOSPAMco.uk
Fri Sep 22 00:31:14 CEST 2000


Alex <cut_me_out at hotmail.com> wrote:

>
>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.

You're working well tonight!
Thanks.
I'll think on that one.

Dale Strickland-Clark
Out-Think Ltd
Business Technology Consultants




More information about the Python-list mailing list