How do I isolate dicts from a list if the id is not found in a second list of dicts (in python)?











up vote
0
down vote

favorite












I have two lists of dicts



list1 = 
[
{"name": "Maria",
"id": "16a",
},
{"name": "Tania",
"id": "13b",
},
{"name": "Steve",
"id": "5a",
}
]


list2 =
[
{"name": "Eric",
"id": "16a",
},
{"name": "Mike",
"id": "7b",
},
{"name": "Steve",
id: "57a",
}
]


I want to be able to return a list of dicts from list2, if the same id is not found in list1



For example, it should return



[
{"name": "Mike",
"id": "7b",
},
{"name": "Steve",
"id": "57a",
}
]


I tried a few suggestions here on stack overflow but haven't been able to get it right.










share|improve this question


























    up vote
    0
    down vote

    favorite












    I have two lists of dicts



    list1 = 
    [
    {"name": "Maria",
    "id": "16a",
    },
    {"name": "Tania",
    "id": "13b",
    },
    {"name": "Steve",
    "id": "5a",
    }
    ]


    list2 =
    [
    {"name": "Eric",
    "id": "16a",
    },
    {"name": "Mike",
    "id": "7b",
    },
    {"name": "Steve",
    id: "57a",
    }
    ]


    I want to be able to return a list of dicts from list2, if the same id is not found in list1



    For example, it should return



    [
    {"name": "Mike",
    "id": "7b",
    },
    {"name": "Steve",
    "id": "57a",
    }
    ]


    I tried a few suggestions here on stack overflow but haven't been able to get it right.










    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I have two lists of dicts



      list1 = 
      [
      {"name": "Maria",
      "id": "16a",
      },
      {"name": "Tania",
      "id": "13b",
      },
      {"name": "Steve",
      "id": "5a",
      }
      ]


      list2 =
      [
      {"name": "Eric",
      "id": "16a",
      },
      {"name": "Mike",
      "id": "7b",
      },
      {"name": "Steve",
      id: "57a",
      }
      ]


      I want to be able to return a list of dicts from list2, if the same id is not found in list1



      For example, it should return



      [
      {"name": "Mike",
      "id": "7b",
      },
      {"name": "Steve",
      "id": "57a",
      }
      ]


      I tried a few suggestions here on stack overflow but haven't been able to get it right.










      share|improve this question













      I have two lists of dicts



      list1 = 
      [
      {"name": "Maria",
      "id": "16a",
      },
      {"name": "Tania",
      "id": "13b",
      },
      {"name": "Steve",
      "id": "5a",
      }
      ]


      list2 =
      [
      {"name": "Eric",
      "id": "16a",
      },
      {"name": "Mike",
      "id": "7b",
      },
      {"name": "Steve",
      id: "57a",
      }
      ]


      I want to be able to return a list of dicts from list2, if the same id is not found in list1



      For example, it should return



      [
      {"name": "Mike",
      "id": "7b",
      },
      {"name": "Steve",
      "id": "57a",
      }
      ]


      I tried a few suggestions here on stack overflow but haven't been able to get it right.







      python






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 8 at 16:43









      rose

      2391310




      2391310
























          3 Answers
          3






          active

          oldest

          votes

















          up vote
          3
          down vote



          accepted










          Use a list-comprehension that iterates through list2 checking the id with ids in list1:



          list1 = [
          {'name': "Maria",
          'id': "16a",
          },
          {'name': "Tania",
          'id': "13b",
          },
          {'name': "Steve",
          'id': "5a",
          }
          ]

          list2 = [
          {'name': "Eric",
          'id': "16a",
          },
          {'name': "Mike",
          'id': "7b",
          },
          {'name': "Steve",
          'id': "57a",
          }
          ]

          list1_ids = [y['id'] for y in list1]
          result = [x for x in list2 if x['id'] not in list1_ids]
          # [{'name': 'Mike', 'id': '7b'}, {'name': 'Steve', 'id': '57a'}]





          share|improve this answer



















          • 1




            It's probably worthwhile to mention that Vasilis G's answer is preferable in terms of performance (O(1) vs O(N)).
            – Demian Brecht
            Nov 8 at 18:12


















          up vote
          1
          down vote













          This should do:



          [d2 for d2 in list2 if d2['id'] not in [d1['id'] for d1 in list1]]


          Output:



          [{'id': '7b', 'name': 'Mike'}, {'id': '57a', 'name': 'Steve'}]





          share|improve this answer























          • Copy of answer 1
            – Ian Kirkpatrick
            Nov 8 at 16:54










          • @IanKirkpatrick didn't even check the answers
            – Alexandre Nixon
            Nov 8 at 16:55


















          up vote
          1
          down vote













          You can also do it using filter function:



          list1 = [
          {"name": "Maria",
          "id": "16a",
          },
          {"name": "Tania",
          "id": "13b",
          },
          {"name": "Steve",
          "id": "5a",
          }
          ]


          list2 = [
          {"name": "Eric",
          "id": "16a",
          },
          {"name": "Mike",
          "id": "7b",
          },
          {"name": "Steve",
          "id": "57a",
          }
          ]

          IDs = set(value["id"] for value in list1)
          output = list(filter(lambda elem: elem["id"] not in IDs, list2))
          print(output)


          Output:



          [{'name': 'Mike', 'id': '7b'}, {'name': 'Steve', 'id': '57a'}]





          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














             

            draft saved


            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53212328%2fhow-do-i-isolate-dicts-from-a-list-if-the-id-is-not-found-in-a-second-list-of-di%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            3 Answers
            3






            active

            oldest

            votes








            3 Answers
            3






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            3
            down vote



            accepted










            Use a list-comprehension that iterates through list2 checking the id with ids in list1:



            list1 = [
            {'name': "Maria",
            'id': "16a",
            },
            {'name': "Tania",
            'id': "13b",
            },
            {'name': "Steve",
            'id': "5a",
            }
            ]

            list2 = [
            {'name': "Eric",
            'id': "16a",
            },
            {'name': "Mike",
            'id': "7b",
            },
            {'name': "Steve",
            'id': "57a",
            }
            ]

            list1_ids = [y['id'] for y in list1]
            result = [x for x in list2 if x['id'] not in list1_ids]
            # [{'name': 'Mike', 'id': '7b'}, {'name': 'Steve', 'id': '57a'}]





            share|improve this answer



















            • 1




              It's probably worthwhile to mention that Vasilis G's answer is preferable in terms of performance (O(1) vs O(N)).
              – Demian Brecht
              Nov 8 at 18:12















            up vote
            3
            down vote



            accepted










            Use a list-comprehension that iterates through list2 checking the id with ids in list1:



            list1 = [
            {'name': "Maria",
            'id': "16a",
            },
            {'name': "Tania",
            'id': "13b",
            },
            {'name': "Steve",
            'id': "5a",
            }
            ]

            list2 = [
            {'name': "Eric",
            'id': "16a",
            },
            {'name': "Mike",
            'id': "7b",
            },
            {'name': "Steve",
            'id': "57a",
            }
            ]

            list1_ids = [y['id'] for y in list1]
            result = [x for x in list2 if x['id'] not in list1_ids]
            # [{'name': 'Mike', 'id': '7b'}, {'name': 'Steve', 'id': '57a'}]





            share|improve this answer



















            • 1




              It's probably worthwhile to mention that Vasilis G's answer is preferable in terms of performance (O(1) vs O(N)).
              – Demian Brecht
              Nov 8 at 18:12













            up vote
            3
            down vote



            accepted







            up vote
            3
            down vote



            accepted






            Use a list-comprehension that iterates through list2 checking the id with ids in list1:



            list1 = [
            {'name': "Maria",
            'id': "16a",
            },
            {'name': "Tania",
            'id': "13b",
            },
            {'name': "Steve",
            'id': "5a",
            }
            ]

            list2 = [
            {'name': "Eric",
            'id': "16a",
            },
            {'name': "Mike",
            'id': "7b",
            },
            {'name': "Steve",
            'id': "57a",
            }
            ]

            list1_ids = [y['id'] for y in list1]
            result = [x for x in list2 if x['id'] not in list1_ids]
            # [{'name': 'Mike', 'id': '7b'}, {'name': 'Steve', 'id': '57a'}]





            share|improve this answer














            Use a list-comprehension that iterates through list2 checking the id with ids in list1:



            list1 = [
            {'name': "Maria",
            'id': "16a",
            },
            {'name': "Tania",
            'id': "13b",
            },
            {'name': "Steve",
            'id': "5a",
            }
            ]

            list2 = [
            {'name': "Eric",
            'id': "16a",
            },
            {'name': "Mike",
            'id': "7b",
            },
            {'name': "Steve",
            'id': "57a",
            }
            ]

            list1_ids = [y['id'] for y in list1]
            result = [x for x in list2 if x['id'] not in list1_ids]
            # [{'name': 'Mike', 'id': '7b'}, {'name': 'Steve', 'id': '57a'}]






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 8 at 17:08

























            answered Nov 8 at 16:48









            Austin

            8,3563828




            8,3563828








            • 1




              It's probably worthwhile to mention that Vasilis G's answer is preferable in terms of performance (O(1) vs O(N)).
              – Demian Brecht
              Nov 8 at 18:12














            • 1




              It's probably worthwhile to mention that Vasilis G's answer is preferable in terms of performance (O(1) vs O(N)).
              – Demian Brecht
              Nov 8 at 18:12








            1




            1




            It's probably worthwhile to mention that Vasilis G's answer is preferable in terms of performance (O(1) vs O(N)).
            – Demian Brecht
            Nov 8 at 18:12




            It's probably worthwhile to mention that Vasilis G's answer is preferable in terms of performance (O(1) vs O(N)).
            – Demian Brecht
            Nov 8 at 18:12












            up vote
            1
            down vote













            This should do:



            [d2 for d2 in list2 if d2['id'] not in [d1['id'] for d1 in list1]]


            Output:



            [{'id': '7b', 'name': 'Mike'}, {'id': '57a', 'name': 'Steve'}]





            share|improve this answer























            • Copy of answer 1
              – Ian Kirkpatrick
              Nov 8 at 16:54










            • @IanKirkpatrick didn't even check the answers
              – Alexandre Nixon
              Nov 8 at 16:55















            up vote
            1
            down vote













            This should do:



            [d2 for d2 in list2 if d2['id'] not in [d1['id'] for d1 in list1]]


            Output:



            [{'id': '7b', 'name': 'Mike'}, {'id': '57a', 'name': 'Steve'}]





            share|improve this answer























            • Copy of answer 1
              – Ian Kirkpatrick
              Nov 8 at 16:54










            • @IanKirkpatrick didn't even check the answers
              – Alexandre Nixon
              Nov 8 at 16:55













            up vote
            1
            down vote










            up vote
            1
            down vote









            This should do:



            [d2 for d2 in list2 if d2['id'] not in [d1['id'] for d1 in list1]]


            Output:



            [{'id': '7b', 'name': 'Mike'}, {'id': '57a', 'name': 'Steve'}]





            share|improve this answer














            This should do:



            [d2 for d2 in list2 if d2['id'] not in [d1['id'] for d1 in list1]]


            Output:



            [{'id': '7b', 'name': 'Mike'}, {'id': '57a', 'name': 'Steve'}]






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 8 at 16:55

























            answered Nov 8 at 16:53









            Alexandre Nixon

            47611




            47611












            • Copy of answer 1
              – Ian Kirkpatrick
              Nov 8 at 16:54










            • @IanKirkpatrick didn't even check the answers
              – Alexandre Nixon
              Nov 8 at 16:55


















            • Copy of answer 1
              – Ian Kirkpatrick
              Nov 8 at 16:54










            • @IanKirkpatrick didn't even check the answers
              – Alexandre Nixon
              Nov 8 at 16:55
















            Copy of answer 1
            – Ian Kirkpatrick
            Nov 8 at 16:54




            Copy of answer 1
            – Ian Kirkpatrick
            Nov 8 at 16:54












            @IanKirkpatrick didn't even check the answers
            – Alexandre Nixon
            Nov 8 at 16:55




            @IanKirkpatrick didn't even check the answers
            – Alexandre Nixon
            Nov 8 at 16:55










            up vote
            1
            down vote













            You can also do it using filter function:



            list1 = [
            {"name": "Maria",
            "id": "16a",
            },
            {"name": "Tania",
            "id": "13b",
            },
            {"name": "Steve",
            "id": "5a",
            }
            ]


            list2 = [
            {"name": "Eric",
            "id": "16a",
            },
            {"name": "Mike",
            "id": "7b",
            },
            {"name": "Steve",
            "id": "57a",
            }
            ]

            IDs = set(value["id"] for value in list1)
            output = list(filter(lambda elem: elem["id"] not in IDs, list2))
            print(output)


            Output:



            [{'name': 'Mike', 'id': '7b'}, {'name': 'Steve', 'id': '57a'}]





            share|improve this answer



























              up vote
              1
              down vote













              You can also do it using filter function:



              list1 = [
              {"name": "Maria",
              "id": "16a",
              },
              {"name": "Tania",
              "id": "13b",
              },
              {"name": "Steve",
              "id": "5a",
              }
              ]


              list2 = [
              {"name": "Eric",
              "id": "16a",
              },
              {"name": "Mike",
              "id": "7b",
              },
              {"name": "Steve",
              "id": "57a",
              }
              ]

              IDs = set(value["id"] for value in list1)
              output = list(filter(lambda elem: elem["id"] not in IDs, list2))
              print(output)


              Output:



              [{'name': 'Mike', 'id': '7b'}, {'name': 'Steve', 'id': '57a'}]





              share|improve this answer

























                up vote
                1
                down vote










                up vote
                1
                down vote









                You can also do it using filter function:



                list1 = [
                {"name": "Maria",
                "id": "16a",
                },
                {"name": "Tania",
                "id": "13b",
                },
                {"name": "Steve",
                "id": "5a",
                }
                ]


                list2 = [
                {"name": "Eric",
                "id": "16a",
                },
                {"name": "Mike",
                "id": "7b",
                },
                {"name": "Steve",
                "id": "57a",
                }
                ]

                IDs = set(value["id"] for value in list1)
                output = list(filter(lambda elem: elem["id"] not in IDs, list2))
                print(output)


                Output:



                [{'name': 'Mike', 'id': '7b'}, {'name': 'Steve', 'id': '57a'}]





                share|improve this answer














                You can also do it using filter function:



                list1 = [
                {"name": "Maria",
                "id": "16a",
                },
                {"name": "Tania",
                "id": "13b",
                },
                {"name": "Steve",
                "id": "5a",
                }
                ]


                list2 = [
                {"name": "Eric",
                "id": "16a",
                },
                {"name": "Mike",
                "id": "7b",
                },
                {"name": "Steve",
                "id": "57a",
                }
                ]

                IDs = set(value["id"] for value in list1)
                output = list(filter(lambda elem: elem["id"] not in IDs, list2))
                print(output)


                Output:



                [{'name': 'Mike', 'id': '7b'}, {'name': 'Steve', 'id': '57a'}]






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 8 at 16:58

























                answered Nov 8 at 16:52









                Vasilis G.

                2,6492621




                2,6492621






























                     

                    draft saved


                    draft discarded



















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53212328%2fhow-do-i-isolate-dicts-from-a-list-if-the-id-is-not-found-in-a-second-list-of-di%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Landwehr

                    Reims

                    Schenkenzell