简体   繁体   中英

Find and remove the dict from one of two lists of dicts if there is a key-value pair not present in at least one of the lists

After certain manipulations I get two lists of dictionaries sorted by numeric_id key. lets'say I have

list1 = [
        {'ref': 'link1', 'numeric_id': 1},
        {'ref': 'link2', 'numeric_id': 2},
        {'ref': 'link3', 'numeric_id': 3},
        {'ref': 'link4', 'numeric_id': 4},
        {'ref': 'link5', 'numeric_id': 5}
]

list2 = [
        {'ref': 'different_link1', 'numeric_id': 1},
        {'ref': 'different_link2', 'numeric_id': 2},
        {'ref': 'different_link4', 'numeric_id': 4},
        {'ref': 'different_link5', 'numeric_id': 5}
]

And in the second list the value 3 in "numeric_id" key is not present while the first list contains such key-value pair. Then I have to remove this dictionary from the list 1 as I need to have only matching pairs based on numeric_id in both lists. Also can be the opposite case, when the value is not present in the first list, while it is in the second one. I cannot know what will be the case beforehand.

The result should be two list without any unpaired elements. Because the lists contain dictionaries with different links, the only connection between them is the value of numeric_id key

The task seemed to be quite easy but I'm already quite lost. Could you please help? Found a lot of seamingly similar questions but couldn't find the proper solution for my case.

Thanks in advance!

You can use filter :

list1 = [{'numeric_id': 1, 'ref': 'link1'}, {'numeric_id': 2, 'ref': 'link2'}, {'numeric_id': 3, 'ref': 'link3'}, {'numeric_id': 4, 'ref': 'link4'}, {'numeric_id': 5, 'ref': 'link5'}]
list2 = [{'numeric_id': 1, 'ref': 'link1'}, {'numeric_id': 2, 'ref': 'link2'}, {'numeric_id': 4, 'ref': 'link4'}, {'numeric_id': 5, 'ref': 'link5'}]
new_list1 = list(filter(lambda x:any(c['numeric_id'] == x['numeric_id'] for c in list2), list1))
new_list2 = list(filter(lambda x:any(c['numeric_id'] == x['numeric_id'] for c in list1), list2))

Output:

[{'numeric_id': 1, 'ref': 'link1'}, {'numeric_id': 2, 'ref': 'link2'}, {'numeric_id': 4, 'ref': 'link4'}, {'numeric_id': 5, 'ref': 'link5'}]
[{'numeric_id': 1, 'ref': 'link1'}, {'numeric_id': 2, 'ref': 'link2'}, {'numeric_id': 4, 'ref': 'link4'}, {'numeric_id': 5, 'ref': 'link5'}]

Maybe you can try this :
1. extract numeric_id from list2
2. append each element of list1 if its numeric_id is in list2

list2_numeric_id = [element["numeric_id"] for element in list2]
[element for element in list1 if element["numeric_id"] in list2_numeric_id]
>> [{'numeric_id': 1, 'ref': 'link1'}, {'numeric_id': 2, 'ref': 'link2'}, {'numeric_id': 4, 'ref': 'link4'}, {'numeric_id': 5, 'ref': 'link5'}]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM