case insensitive dictionary
vbgunz at gmail.com
Sun Nov 26 01:10:49 CET 2006
John Henry wrote:
> I believe the standard dictionary should be amened to allow the use of
> case insensitive keys - as an option. I found some work done by others
> to do that at:
> but the problem with that approach is that they lowercase the keys
> immediately when you create the dictionary and so the true identity of
> the key is lost.
> Of course, I can subclass it and save a copy of the "real" key but
> that's kind of messcy.
> In other words:
> If I have:
> pets["Cat"] = 3
> pets["Dog"] = 2
> I would like to see:
> pets["cat"] prints 3
> pets["DOG"] prints 2
> print pets.keys()
> should print:
> "Cat", "Dog"
> "cat", "dog"
You can try to title-case the list returned with keys() like so:
print [x.title() for x in pets.keys()]
Not a perfect solution but you get what you want... just an idea :)
More information about the Python-list