Skip to content

Instantly share code, notes, and snippets.

@wrboyce
Created December 7, 2011 10:52
Show Gist options
  • Select an option

  • Save wrboyce/1442375 to your computer and use it in GitHub Desktop.

Select an option

Save wrboyce/1442375 to your computer and use it in GitHub Desktop.
def diff_lists(old_list, new_list):
"""
>>> diff_lists([1,2,3], [2,3,4]) # removed 1, added 4
[('-', 1), ('+', 4)]
>>> diff_lists([1,2,3], [1,2,3])
[]
"""
old = set(old_list)
new = set(new_list)
return [('-', e) for e in old-new] + [('+', e) for e in new-old]
if __name__ == "__main__":
import doctest
doctest.testmod()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment