Skip to content

Instantly share code, notes, and snippets.

@smileboywtu
Created December 31, 2016 03:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save smileboywtu/83c5ca8047db5c91ad284243e93711fe to your computer and use it in GitHub Desktop.
Save smileboywtu/83c5ca8047db5c91ad284243e93711fe to your computer and use it in GitHub Desktop.
a way to find out data update according to primary key
#!/usr/bin/env python
list1 = [
{"id": 1, "name": "smile", "age": 23},
{"id": 3, "name": "mary", "age": 40},
{"id": 4, "name": "cherry", "age": 67}
]
list2 = [
{"id": 1, "name": "smile", "age": 23},
{"id": 3, "name": "Json", "age": 40},
{"id": 4, "name": "cherry", "age": 67}
]
dict1 = { item["id"]: item for item in list1 }
dict2 = { item["id"]: item for item in list2 }
updates = { key: dict2[key] for key in dict2 if dict1[key] != dict2[key] }
ret = updates.values()
print dict1
print dict2
print ret
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment