By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,206 Members | 1,050 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,206 IT Pros & Developers. It's quick & easy.

compare dictionary values

P: n/a
rbt
What's a good way to compare values in dictionaries? I want to find
values that have changed. I look for new keys by doing this:

new = [k for k in file_info_cur.iterkeys() if k not in
file_info_old.iterkeys()]
if new == []:
print new, "No new files."
else:
print new, "New file(s)!!!"

My key-values pairs are filepaths and their modify times. I want to
identify files that have been updated or added since the script last ran.

Thanks,
rbt
Dec 30 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
In <dp**********@solaris.cc.vt.edu>, rbt wrote:
What's a good way to compare values in dictionaries?
Look them up and then compare!? ;-)
I want to find
values that have changed. I look for new keys by doing this:

new = [k for k in file_info_cur.iterkeys() if k not in
file_info_old.iterkeys()]
if new == []:
print new, "No new files."
else:
print new, "New file(s)!!!"

My key-values pairs are filepaths and their modify times. I want to
identify files that have been updated or added since the script last ran.


This looks up each `key` from the `new` dictionary and compares the value
with the `old` one. If it's not equal or the key is not present in `old`
the key is appended to the `result`::

def new_and_changed_keys(old, new):
result = list()
for (key, value) in new:
try:
if old[key] != value:
result.append(key)
except KeyError:
result.append(key)
return result

Ciao,
Marc 'BlackJack' Rintsch
Dec 30 '05 #2

P: n/a
rbt
Marc 'BlackJack' Rintsch wrote:
In <dp**********@solaris.cc.vt.edu>, rbt wrote:
What's a good way to compare values in dictionaries?


Look them up and then compare!? ;-)
I want to find
values that have changed. I look for new keys by doing this:

new = [k for k in file_info_cur.iterkeys() if k not in
file_info_old.iterkeys()]
if new == []:
print new, "No new files."
else:
print new, "New file(s)!!!"

My key-values pairs are filepaths and their modify times. I want to
identify files that have been updated or added since the script last ran.


This looks up each `key` from the `new` dictionary and compares the value
with the `old` one. If it's not equal or the key is not present in `old`
the key is appended to the `result`::

def new_and_changed_keys(old, new):
result = list()
for (key, value) in new:
try:
if old[key] != value:
result.append(key)
except KeyError:
result.append(key)
return result

Ciao,
Marc 'BlackJack' Rintsch


Thanks Marc! I changed this line:

for (key, value) in new:

To this:

for (key, value) in new.iteritems():

And, it works great. Thanks again.
Dec 30 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.