mattia wrote:
Il Tue, 22 Dec 2009 23:09:04 +0100, Peter Otten ha scritto:
mattia wrote:
Is there a function to initialize a dictionary? Right now I'm using:
d = {x+1:[] for x in range(50)}
Is there any better solution?
There is a dictionary variant that you don't have to initialize:
from co
On Dec 22, 11:51 pm, mattia wrote:
> Il Tue, 22 Dec 2009 23:09:04 +0100, Peter Otten ha scritto:
>
> > mattia wrote:
>
> >> Is there a function to initialize a dictionary? Right now I'm using:
> >> d = {x+1:[] for x in range(50)}
> >> Is there any better solution?
>
> > There is a dictionary varia
Il Tue, 22 Dec 2009 23:09:04 +0100, Peter Otten ha scritto:
> mattia wrote:
>
>> Is there a function to initialize a dictionary? Right now I'm using:
>> d = {x+1:[] for x in range(50)}
>> Is there any better solution?
>
> There is a dictionary variant that you don't have to initialize:
>
> from
Il Tue, 22 Dec 2009 23:09:04 +0100, Peter Otten ha scritto:
> mattia wrote:
>
>> Is there a function to initialize a dictionary? Right now I'm using:
>> d = {x+1:[] for x in range(50)}
>> Is there any better solution?
>
> There is a dictionary variant that you don't have to initialize:
>
> from
mattia wrote:
> Is there a function to initialize a dictionary?
> Right now I'm using:
> d = {x+1:[] for x in range(50)}
> Is there any better solution?
There is a dictionary variant that you don't have to initialize:
from collections import defaultdict
d = defaultdict(list)
Peter
--
http://ma
"mattia" wrote in message
news:[email protected]...
Is there a function to initialize a dictionary?
Right now I'm using:
d = {x+1:[] for x in range(50)}
Is there any better solution?
Depending on your use case, a defaultdict might suite you:
from collections impo
On 22-12-2009 22:33, mattia wrote:
Is there a function to initialize a dictionary?
Right now I'm using:
d = {x+1:[] for x in range(50)}
Is there any better solution?
Maybe you can use:
dict.fromkeys(xrange(1,51))
but this will initialize all values to None instead of an empty list...
-
On 12/22/2009 1:33 PM mattia said...
Is there a function to initialize a dictionary?
Right now I'm using:
d = {x+1:[] for x in range(50)}
Is there any better solution?
I tend to use setdefault and fill in as I go, but if you need to have a
complete 50-element dict from the get go, I'd probably
On 2009-12-22 15:33 PM, mattia wrote:
Is there a function to initialize a dictionary?
Right now I'm using:
d = {x+1:[] for x in range(50)}
Is there any better solution?
For things like this? No. If you find yourself writing this pattern frequently,
though, you can wrap it up in a function and