[Tutor] changing dictionary to lowercase

Joel Goldstick joel.goldstick at gmail.com
Thu Oct 27 20:36:04 CEST 2011


On Thu, Oct 27, 2011 at 2:25 PM, ADRIAN KELLY <kellyadrian at hotmail.com>wrote:

>
> Hi all,
> is it possible to change a dictionary list to lowercase..without having to
> retype?
> e.g. definitions={"Deprecated": "No longer in use", "Depreciation": "fall
> in value of an asset"}
>
> i have tried definitions=definitions.lower()
>
> regards
> adrian
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
> There is a string method called lower so 'Bob'.lower() will return 'bob'

You can't alter the keys in a dictionary because they are immutable -- they
can't be changed

But you can loop through your dictionary, make new keys lowercase and copy
the values associated with each key

like this:

>>> new_d = {}

>>> for d in definitions:
...   new_d[d.lower()] = definitions[d]
...
>>> new_d
{'deprecated': 'No longer in use', 'depreciation': 'fall in value of an
asset'}
>>>




-- 
Joel Goldstick
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20111027/912f1e61/attachment.html>


More information about the Tutor mailing list