Concise idiom to initialize dictionaries
bruno modulix
onurb at xiludom.gro
Thu Nov 11 14:31:28 EST 2004
Larry Bates a écrit :
> It is almost certain that you should use either a list of dictionaries
> or a dictionary containing other dictionaries for this. Creation of
> 26 distinct dictionaries is almost never a good solution as it makes
> it nearly impossible to iterate over them and would make code to
> manipulate them unable to be generalized easily.
>
> Example:
>
> #
> # To create a list of dictionaries
> #
> list_of_dicts=[]
> for i in range(26):
> list_of_dicts.append({})
>
or just
list_of_dicts = [{} for i in range(26)]
or if you want a dict of dicts :
dod = dict([(i, {}) for i in range(26)])
More information about the Python-list
mailing list