I have two list of dictionaries representing the rows of two tables, so:
tableA = [{"id": 1, "name": "foo"}, {"id": 2, "name": "bar"}]
tableB = [{"id": 1, "name": "bar"}, {"id": 3, "name": "baz"}]
I want to obtain the difference in the following way:
added = [{"id": 3, "name": "baz"}]
updated = [{"id": 1, "name": "bar"}]
I know, that id
is unique.
So, I am planning to loop over tableB
, ask for the id
, if they are equal then compare the dicts to know if it is updated
, if not the id is new and it is added
.
for x in tableA:
idexists = false
for y in tableY:
if x["id"] == y["id"]:
if x != y:
updated.append(x)
idexists = true
if not idexists:
added.append(x)
Is it correct? Can it be done in pythonic way?