[Tutor] changing dictionary to lowercase

Albert-Jan Roskam fomcl at yahoo.com
Fri Oct 28 11:51:39 CEST 2011


It would be nice to generalize the solution so it could also handle
definitions={"Deprecated": "No longer in use", "DEPRECATED":  "No longer in use"}
These are unique now, but after turning them into lower case not anymore. 
new_d = {}
for d in definitions:
    try:
        new_d[d.lower()].append(definitions[d])
    except TypeError:
        new_d[d.lower()] = [definitions[d]]
 

Cheers!!
Albert-Jan


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a fresh water system, and public health, what have the Romans ever done for us?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


>________________________________
>From: Joel Goldstick <joel.goldstick at gmail.com>
>To: tutor at python.org
>Sent: Thursday, October 27, 2011 8:36 PM
>Subject: Re: [Tutor] changing dictionary to lowercase
>
>
>
>
>
>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
>
>
>_______________________________________________
>Tutor maillist  -  Tutor at python.org
>To unsubscribe or change subscription options:
>http://mail.python.org/mailman/listinfo/tutor
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20111028/309967a1/attachment.html>


More information about the Tutor mailing list