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
updating dictionaries from/to 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="Steven D'Aprano, post: 3609694"] The old way: foo = {} for key in all_the_keys: foo[key] = 1 The new way: foo = dict.fromkeys(all_the_keys, 1) Harder to say what you want to do than to just do it. The long way: for key in foo: if bar.has_key(key): foo[key] = foo[key] + bar[key] Probably a better way: for key, value in foo.iteritems(): foo[key] = value + bar.get(key, 0) You should also investigate the update method of dictionaries. From an interactive session, type: help({}.update) then the Enter key. [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
Python
updating dictionaries from/to dictionaries
Top