Created
December 31, 2016 03:21
-
-
Save smileboywtu/83c5ca8047db5c91ad284243e93711fe to your computer and use it in GitHub Desktop.
a way to find out data update according to primary key
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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