AlexW
AlexW

Reputation: 2589

python compare two dict lists and show missing items

I have two lists

list_1 =  [{ "id" : 1, "name" : "kitten"}, { "id" : 2, "name" : "puppy"}, { "id" : 2, "name" : "mouse"}]

list_2 =  [{ "id" : 1, "name" : "kitten"}, { "id" : 2, "name" : "mouse"}]

I would like to have a new list created that shows items missing from list_2. so list_3 for example would show

list 3 = { "id" : 2, "name" : "puppy"}

Would someone be able to help me achieve this?

Thanks

Upvotes: 0

Views: 71

Answers (1)

politinsa
politinsa

Reputation: 3720

In [1]: list_1 =  [{ "id" : 1, "name" : "kitten"}, { "id" : 2, "name" : "puppy"}, { "id" : 2, "name" : "mouse"}]
   ...:
   ...: list_2 =  [{ "id" : 1, "name" : "kitten"}, { "id" : 2, "name" : "mouse"}]

In [2]: [i for i in list_1 if i not in list_2]
Out[2]: [{'id': 2, 'name': 'puppy'}]

Upvotes: 3

Related Questions