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

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

css - Firefox for ubuntu renders wrong colors -