I have two lists of dictonaries. They contain gmail labels and filters. I need to get differences between them in form of two differences lists. I want order of the elements to be preserved.
Example:
a=[
{'name': 'label_a'},
{'color': {'background': '#42d692', 'text': '#094228'}, 'name': 'label_b'},
{'name': 'label_d'},
{'name': 'label_e'},
]
b=[
{'name': 'label_a'},
{'name': 'label_b'},
{'name': 'label_c'},
{'name': 'label_d'},
]
Expected results:
a_diff=[
{'color': {'background': '#42d692', 'text': '#094228'}, 'name': 'label_b'},
{'name': 'label_e'},
]
b_diff=[
{'name': 'label_b'},
{'name': 'label_c'},
]
This is how I could do this, but I think it might be inefficient. Can I do something better?
a_diff=[]
b_diff=[]
for i in a:
if i not in b:
a_diff.append(i)
for j in b:
if j not in a:
b_diff.append(j)
I don't need to keep original lists after this operation, so this can be in-place. Speed is most important. So this question can be also rephrased as: "How to remove common elements between two lists from those lists?"
When you provide solution, let know if it would work also on any two lists or only lists of dictonaries.
Also please let know if your solution will remove duplicates or not (I am interested in both kinds of solutions):
a=[
{'name': 'label_a'},
{'name': 'label_a'},
{'name': 'label_b'},
]
b=[
{'name': 'label_a'},
{'name': 'label_b'},
{'name': 'label_c'},
]
Results with removed duplicate.
a_diff=[
]
b_diff=[
{'name': 'label_c'},
]
Results without removed duplicate.
a_diff=[
{'name': 'label_a'},
]
b_diff=[
{'name': 'label_c'},
]
@MoritzPlenz
Thanks.
This question was posted (after modifications) on SO: https://stackoverflow.com/questions/68091970/get-differences-between-two-lists-of-dictionaries-result-as-two-differences-lis
I updated description of this gist to make it clear.
If you want, you can post your solution as another answer.