python - Finding the difference when substracting two dictionaries in both ways in one operation -
i have code:
new = {'a': 1, 'b': 2} old = {'a': 1, 'c': 3} added = new.keys() - old.keys() if added: print('{} keys have been added'.format(len(added))) removed = old.keys() - new.keys() if removed: print('{} keys have been removed'.format(len(removed))) # added, removed = minus_dict(new, old)
i have subtraction operation twice. minus_dict function exist? mean how find added , removed in more efficient way?
use xor operator if single set suffices:
>>> old.keys() ^ new.keys() {'c', 'b'}
if that's not enough, have 2 subtractions, or code algorithm yourself.
Comments
Post a Comment