I was wondering if you can help me.
I have two lists that contain dictionaries, for the most part these keys are the same. The following is a brief example:
x1 = [{'a':1,'b':"cat"},{'a':2,'b':"parrot"},...]
x2 = [{'a':2,'b':"dog"},{'a':1,'b':"fish"},...]
Now I would like to compare values based on the key, that is, the key a, but the length of both lists will not always be the same. The key a will always be in both dictionaries, if there is a corresponding dictionary ie x1[0]['a'] == x2[1]['a'].
How can I compare these dictionaries based on the key a, so that I can first discard those x1that do not appear in x2, vice versa. Then determine if certain values appear in both dictionaries and then write them to the database, this is not necessary here.
What I thought was to combine these dictionaries into a tuple in a list based on a key. Then repeat this and compare these values. This is probably not the best way to do this, so if you have any better ideas, please feel free to. :)
[Edit].
I did not clearly formulate this question, sorry. What I hope to do; first: match dictionaries based on key a. Second: ignore those that do not match (key a). Third: compare key b. fourth: update the database based on the comparison b.
Thanks to all who responded.
My answer would be something like this:
"I thought a comp list could do well to build a tuple containing a dictionary from x1, which matches a dictionary from x2, and then iterate over each element of comparison b, but I thought it might be too slow."
, . :)
.