[Tutor] Dictionary get method

Mitya Sirenef msirenef at lightbird.net
Wed Mar 20 06:26:23 CET 2013


On 03/20/2013 01:09 AM, Mitya Sirenef wrote:
>
>
> By the way, you can further simplify it by doing:
>
> def histogram2(s):
>     return {c: d.get(c,0)+1 for c in s}
>
>
> That will work in python 3, in python 2 you need:
>
>     return dict((c: d.get(c,0)+1) for c in s)
>
>
Sorry, it should use a comma:

return dict((c, d.get(c,0)+1) for c in s)

-m


-- 
Lark's Tongue Guide to Python: http://lightbird.net/larks/



More information about the Tutor mailing list