dict initialization

Peter Otten __peter__ at web.de
Tue Dec 22 17:09:04 EST 2009


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



More information about the Python-list mailing list