Init a dictionary with a empty lists

Daniel Urban urban.dani at gmail.com
Sat Feb 5 08:38:29 EST 2011


On Sat, Feb 5, 2011 at 14:08, 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 = {0 for i in range(1,MAX_PHRASES_LENGTH+1)}
But I suggest using a defaultdict:
http://docs.python.org/dev/py3k/library/collections#defaultdict-objects

Daniel



More information about the Python-list mailing list