Init a dictionary with a empty lists

Shashwat Anand anand.shashwat at gmail.com
Sat Feb 5 08:34:40 EST 2011


On Sat, Feb 5, 2011 at 6:38 PM, Lisa Fritz Barry Griffin <
lisaochbarry at gmail.com> wrote:

> Hi there,
>
> How can I do this in a one liner:
>
>        maxCountPerPhraseWordLength = {}
>        for i in range(1,MAX_PHRASES_LENGTH+1):
>            maxCountPerPhraseWordLength[i] = 0
>

maxCountPerPhraseWordLength = {}
[maxCountPerPhraseWordLength.setdefault(i, 0) for i
in range(1,MAX_PHRASES_LENGTH+1)]

>From setdefaults documentation,
'D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if k not in D'



>
> Thanks!
> --
> http://mail.python.org/mailman/listinfo/python-list
>



-- 
~l0nwlf
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20110205/1c04b347/attachment.html>


More information about the Python-list mailing list