I was wondering if you could help me.
I have two lists that contain dictionaries, for the most part these keys are the same. See below for 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 the values based on a key i.e. 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 i.e. x1[0]['a'] == x2[1]['a'].
How could I compare these dictionaries based on key a so that I could firstly discard those from x1 that do not appear in x2, vise versa. Then determine if certain values appear in both dictionaries and then record this in a database, this is not necessary here.
What I was thinking was to combine these dictionaries into a tuple within a list based on key a. Then iterate through this and compare these values. This is probably not the best way to go about it, so if you have better ideas, please feel free. :)
[Edit.]
I didn't phrase the question clearly, I'm sorry. What I'm hoping to do is; 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 of b.
Thank you to all those who have answered.
My answer would be something like this:
"I thought a list comp might do fine to build a tuple containing a dictionary from x1 that corresponds with the dictionary from x2, then iterate through each tuple comparing key b but I thought it might be too slow."
I don't think it's a very good way of doing it. So that's why I'm here :)
Thank you in advance.