[Tutor] changing dictionary to lowercase

Christian Witts cwitts at compuscan.co.za
Fri Oct 28 12:01:34 CEST 2011


On 2011/10/28 11:51 AM, Albert-Jan Roskam wrote:
> 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
>
>
> <snip>
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
To save yourself the try/except you can use defaultdict which is part of 
the collections module.

from collections import defaultdict
new_d = defaultdict(list)
for key, value in definitions.iteritems():
     new_d[key.lower()].append(value)

-- 

Christian Witts
Python Developer

//
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20111028/8efd07ab/attachment.html>


More information about the Tutor mailing list