Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
Python
Concise idiom to initialize dictionaries
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
[QUOTE="Larry Bates, post: 1788909"] 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({}) # # Now you can reference each dictionary as: # # list_of_dicts[0], list_of_dicts[1], ... # or import string dict_of_dicts={} for letter in string.ascii_lowercase: dict_of_dicts[letter]={} # # Now you can reference each dictionary as: # # dict_of_dicts['a'], dict_of_dicts['b'], ... Larry Bates [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
Python
Concise idiom to initialize dictionaries
Top