dict initialization
mattia
gervaz at gmail.com
Tue Dec 22 17:33:07 EST 2009
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 collections import defaultdict
> d = defaultdict(list)
>
> Peter
Great, thanks. Now when I call the dict key I also initialize the value,
good also using something like:
if d[n]:
d[n].append(val)
More information about the Python-list
mailing list