Init a dictionary with a empty lists

Daniel Urban urban.dani at gmail.com
Sat Feb 5 16:03:47 CET 2011


On Sat, Feb 5, 2011 at 15:38, Steven D'Aprano
<steve+comp.lang.python at pearwood.info> wrote:
> On Sat, 05 Feb 2011 14:38:29 +0100, Daniel Urban wrote:
>
>> 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)}
>
> Unfortunately not. In versions before Python 2.7, it will give a
> SyntaxError. In 2.7 or better it gives a set instead of a dict:

Yeah, sorry, that should have been  {i: 0 for i in
range(1,MAX_PHRASES_LENGTH+1)}


Daniel



More information about the Python-list mailing list