Python - comparing two dicts values for contains and showing matches





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







-1















I can see lots of similar questions but imo not having much luck finding an answer.



I have two dictionaries with values I want to match against but with different keys. ive attempted a match query but its returning empty. I think its because of the miss matching key names maybe? or not iterating the k,v pairs? But im not sure what to do here



interface_list = [
{'ifIndex': 19, 'Caption': 'GigabitEthernet0/0/0 *** Uplink ***', 'ifType': 131, 'ifSubType': 0, 'InterfaceID': 0, 'Manageable': True, 'ifSpeed': 0.0, 'ifAdminStatus': 0, 'ifOperStatus': 4},
{'ifIndex': 19, 'Caption': 'GigabitEthernet0/0/1', 'ifType': 131, 'ifSubType': 0, 'InterfaceID': 0, 'Manageable': True, 'ifSpeed': 0.0, 'ifAdminStatus': 0, 'ifOperStatus': 4},
{'ifIndex': 19, 'Caption': 'GigabitEthernet0/0/2', 'ifType': 131, 'ifSubType': 0, 'InterfaceID': 0, 'Manageable': True, 'ifSpeed': 0.0, 'ifAdminStatus': 0, 'ifOperStatus': 4},
{'ifIndex': 19, 'Caption': 'Tunnel100', 'ifType': 131, 'ifSubType': 0, 'InterfaceID': 0, 'Manageable': True, 'ifSpeed': 0.0, 'ifAdminStatus': 0, 'ifOperStatus': 4},
{'ifIndex': 20, 'Caption': 'Vlan5', 'ifType': 53, 'ifSubType': 0, 'InterfaceID': 0, 'Manageable': True, 'ifSpeed': 0.0, 'ifAdminStatus': 0, 'ifOperStatus': 4},
{'ifIndex': 21, 'Caption': 'Vlan10', 'ifType': 53, 'ifSubType': 0, 'InterfaceID': 0, 'Manageable': True, 'ifSpeed': 0.0, 'ifAdminStatus': 0, 'ifOperStatus': 4},
{'ifIndex': 22, 'Caption': 'Vlan15', 'ifType': 53, 'ifSubType': 0, 'InterfaceID': 0, 'Manageable': True, 'ifSpeed': 0.0, 'ifAdminStatus': 0, 'ifOperStatus': 4},
]

wanted_interfaces = [{'resource': 'GigabitEthernet0/0/0'}, {'resource': 'Vlan5'}]

>>> matches = [i for str(i) in wanted_interfaces if i in interface_list]
>>> matches



it should hopefully return the record containing 'GigabitEthernet0/0/0 * Uplink *' as a match










share|improve this question































    -1















    I can see lots of similar questions but imo not having much luck finding an answer.



    I have two dictionaries with values I want to match against but with different keys. ive attempted a match query but its returning empty. I think its because of the miss matching key names maybe? or not iterating the k,v pairs? But im not sure what to do here



    interface_list = [
    {'ifIndex': 19, 'Caption': 'GigabitEthernet0/0/0 *** Uplink ***', 'ifType': 131, 'ifSubType': 0, 'InterfaceID': 0, 'Manageable': True, 'ifSpeed': 0.0, 'ifAdminStatus': 0, 'ifOperStatus': 4},
    {'ifIndex': 19, 'Caption': 'GigabitEthernet0/0/1', 'ifType': 131, 'ifSubType': 0, 'InterfaceID': 0, 'Manageable': True, 'ifSpeed': 0.0, 'ifAdminStatus': 0, 'ifOperStatus': 4},
    {'ifIndex': 19, 'Caption': 'GigabitEthernet0/0/2', 'ifType': 131, 'ifSubType': 0, 'InterfaceID': 0, 'Manageable': True, 'ifSpeed': 0.0, 'ifAdminStatus': 0, 'ifOperStatus': 4},
    {'ifIndex': 19, 'Caption': 'Tunnel100', 'ifType': 131, 'ifSubType': 0, 'InterfaceID': 0, 'Manageable': True, 'ifSpeed': 0.0, 'ifAdminStatus': 0, 'ifOperStatus': 4},
    {'ifIndex': 20, 'Caption': 'Vlan5', 'ifType': 53, 'ifSubType': 0, 'InterfaceID': 0, 'Manageable': True, 'ifSpeed': 0.0, 'ifAdminStatus': 0, 'ifOperStatus': 4},
    {'ifIndex': 21, 'Caption': 'Vlan10', 'ifType': 53, 'ifSubType': 0, 'InterfaceID': 0, 'Manageable': True, 'ifSpeed': 0.0, 'ifAdminStatus': 0, 'ifOperStatus': 4},
    {'ifIndex': 22, 'Caption': 'Vlan15', 'ifType': 53, 'ifSubType': 0, 'InterfaceID': 0, 'Manageable': True, 'ifSpeed': 0.0, 'ifAdminStatus': 0, 'ifOperStatus': 4},
    ]

    wanted_interfaces = [{'resource': 'GigabitEthernet0/0/0'}, {'resource': 'Vlan5'}]

    >>> matches = [i for str(i) in wanted_interfaces if i in interface_list]
    >>> matches



    it should hopefully return the record containing 'GigabitEthernet0/0/0 * Uplink *' as a match










    share|improve this question



























      -1












      -1








      -1








      I can see lots of similar questions but imo not having much luck finding an answer.



      I have two dictionaries with values I want to match against but with different keys. ive attempted a match query but its returning empty. I think its because of the miss matching key names maybe? or not iterating the k,v pairs? But im not sure what to do here



      interface_list = [
      {'ifIndex': 19, 'Caption': 'GigabitEthernet0/0/0 *** Uplink ***', 'ifType': 131, 'ifSubType': 0, 'InterfaceID': 0, 'Manageable': True, 'ifSpeed': 0.0, 'ifAdminStatus': 0, 'ifOperStatus': 4},
      {'ifIndex': 19, 'Caption': 'GigabitEthernet0/0/1', 'ifType': 131, 'ifSubType': 0, 'InterfaceID': 0, 'Manageable': True, 'ifSpeed': 0.0, 'ifAdminStatus': 0, 'ifOperStatus': 4},
      {'ifIndex': 19, 'Caption': 'GigabitEthernet0/0/2', 'ifType': 131, 'ifSubType': 0, 'InterfaceID': 0, 'Manageable': True, 'ifSpeed': 0.0, 'ifAdminStatus': 0, 'ifOperStatus': 4},
      {'ifIndex': 19, 'Caption': 'Tunnel100', 'ifType': 131, 'ifSubType': 0, 'InterfaceID': 0, 'Manageable': True, 'ifSpeed': 0.0, 'ifAdminStatus': 0, 'ifOperStatus': 4},
      {'ifIndex': 20, 'Caption': 'Vlan5', 'ifType': 53, 'ifSubType': 0, 'InterfaceID': 0, 'Manageable': True, 'ifSpeed': 0.0, 'ifAdminStatus': 0, 'ifOperStatus': 4},
      {'ifIndex': 21, 'Caption': 'Vlan10', 'ifType': 53, 'ifSubType': 0, 'InterfaceID': 0, 'Manageable': True, 'ifSpeed': 0.0, 'ifAdminStatus': 0, 'ifOperStatus': 4},
      {'ifIndex': 22, 'Caption': 'Vlan15', 'ifType': 53, 'ifSubType': 0, 'InterfaceID': 0, 'Manageable': True, 'ifSpeed': 0.0, 'ifAdminStatus': 0, 'ifOperStatus': 4},
      ]

      wanted_interfaces = [{'resource': 'GigabitEthernet0/0/0'}, {'resource': 'Vlan5'}]

      >>> matches = [i for str(i) in wanted_interfaces if i in interface_list]
      >>> matches



      it should hopefully return the record containing 'GigabitEthernet0/0/0 * Uplink *' as a match










      share|improve this question
















      I can see lots of similar questions but imo not having much luck finding an answer.



      I have two dictionaries with values I want to match against but with different keys. ive attempted a match query but its returning empty. I think its because of the miss matching key names maybe? or not iterating the k,v pairs? But im not sure what to do here



      interface_list = [
      {'ifIndex': 19, 'Caption': 'GigabitEthernet0/0/0 *** Uplink ***', 'ifType': 131, 'ifSubType': 0, 'InterfaceID': 0, 'Manageable': True, 'ifSpeed': 0.0, 'ifAdminStatus': 0, 'ifOperStatus': 4},
      {'ifIndex': 19, 'Caption': 'GigabitEthernet0/0/1', 'ifType': 131, 'ifSubType': 0, 'InterfaceID': 0, 'Manageable': True, 'ifSpeed': 0.0, 'ifAdminStatus': 0, 'ifOperStatus': 4},
      {'ifIndex': 19, 'Caption': 'GigabitEthernet0/0/2', 'ifType': 131, 'ifSubType': 0, 'InterfaceID': 0, 'Manageable': True, 'ifSpeed': 0.0, 'ifAdminStatus': 0, 'ifOperStatus': 4},
      {'ifIndex': 19, 'Caption': 'Tunnel100', 'ifType': 131, 'ifSubType': 0, 'InterfaceID': 0, 'Manageable': True, 'ifSpeed': 0.0, 'ifAdminStatus': 0, 'ifOperStatus': 4},
      {'ifIndex': 20, 'Caption': 'Vlan5', 'ifType': 53, 'ifSubType': 0, 'InterfaceID': 0, 'Manageable': True, 'ifSpeed': 0.0, 'ifAdminStatus': 0, 'ifOperStatus': 4},
      {'ifIndex': 21, 'Caption': 'Vlan10', 'ifType': 53, 'ifSubType': 0, 'InterfaceID': 0, 'Manageable': True, 'ifSpeed': 0.0, 'ifAdminStatus': 0, 'ifOperStatus': 4},
      {'ifIndex': 22, 'Caption': 'Vlan15', 'ifType': 53, 'ifSubType': 0, 'InterfaceID': 0, 'Manageable': True, 'ifSpeed': 0.0, 'ifAdminStatus': 0, 'ifOperStatus': 4},
      ]

      wanted_interfaces = [{'resource': 'GigabitEthernet0/0/0'}, {'resource': 'Vlan5'}]

      >>> matches = [i for str(i) in wanted_interfaces if i in interface_list]
      >>> matches



      it should hopefully return the record containing 'GigabitEthernet0/0/0 * Uplink *' as a match







      python






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 16 '18 at 12:29







      AlexW

















      asked Nov 16 '18 at 12:19









      AlexWAlexW

      51611263




      51611263
























          1 Answer
          1






          active

          oldest

          votes


















          1














          For a comprehensive scan (assuming you want to check every value in every dict in both lists), you would have to do something like:



          matches = [
          v for d1 in interface_list for v in d1.values()
          if any(isinstance(v, str) and vw in v for d2 in wanted_interfaces for vw in d2.values())
          ]
          # ['GigabitEthernet0/0/0 *** Uplink ***']





          share|improve this answer
























          • thank you this works as desired, I would never figured that out

            – AlexW
            Nov 16 '18 at 12:30











          • is it possible to return the full dictionary for the matches?

            – AlexW
            Nov 16 '18 at 12:31













          • Yeah, just start the comprehension with [(d1['ifIndex'], v) for d1 ... instead of [v for d1 ...

            – schwobaseggl
            Nov 16 '18 at 12:34













          • sorry I changed it to the full dictonary is that one possible?

            – AlexW
            Nov 16 '18 at 12:37











          • Try and find out ;) (should work)

            – schwobaseggl
            Nov 16 '18 at 12:38












          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',
          autoActivateHeartbeat: false,
          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%2f53337785%2fpython-comparing-two-dicts-values-for-contains-and-showing-matches%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          For a comprehensive scan (assuming you want to check every value in every dict in both lists), you would have to do something like:



          matches = [
          v for d1 in interface_list for v in d1.values()
          if any(isinstance(v, str) and vw in v for d2 in wanted_interfaces for vw in d2.values())
          ]
          # ['GigabitEthernet0/0/0 *** Uplink ***']





          share|improve this answer
























          • thank you this works as desired, I would never figured that out

            – AlexW
            Nov 16 '18 at 12:30











          • is it possible to return the full dictionary for the matches?

            – AlexW
            Nov 16 '18 at 12:31













          • Yeah, just start the comprehension with [(d1['ifIndex'], v) for d1 ... instead of [v for d1 ...

            – schwobaseggl
            Nov 16 '18 at 12:34













          • sorry I changed it to the full dictonary is that one possible?

            – AlexW
            Nov 16 '18 at 12:37











          • Try and find out ;) (should work)

            – schwobaseggl
            Nov 16 '18 at 12:38
















          1














          For a comprehensive scan (assuming you want to check every value in every dict in both lists), you would have to do something like:



          matches = [
          v for d1 in interface_list for v in d1.values()
          if any(isinstance(v, str) and vw in v for d2 in wanted_interfaces for vw in d2.values())
          ]
          # ['GigabitEthernet0/0/0 *** Uplink ***']





          share|improve this answer
























          • thank you this works as desired, I would never figured that out

            – AlexW
            Nov 16 '18 at 12:30











          • is it possible to return the full dictionary for the matches?

            – AlexW
            Nov 16 '18 at 12:31













          • Yeah, just start the comprehension with [(d1['ifIndex'], v) for d1 ... instead of [v for d1 ...

            – schwobaseggl
            Nov 16 '18 at 12:34













          • sorry I changed it to the full dictonary is that one possible?

            – AlexW
            Nov 16 '18 at 12:37











          • Try and find out ;) (should work)

            – schwobaseggl
            Nov 16 '18 at 12:38














          1












          1








          1







          For a comprehensive scan (assuming you want to check every value in every dict in both lists), you would have to do something like:



          matches = [
          v for d1 in interface_list for v in d1.values()
          if any(isinstance(v, str) and vw in v for d2 in wanted_interfaces for vw in d2.values())
          ]
          # ['GigabitEthernet0/0/0 *** Uplink ***']





          share|improve this answer













          For a comprehensive scan (assuming you want to check every value in every dict in both lists), you would have to do something like:



          matches = [
          v for d1 in interface_list for v in d1.values()
          if any(isinstance(v, str) and vw in v for d2 in wanted_interfaces for vw in d2.values())
          ]
          # ['GigabitEthernet0/0/0 *** Uplink ***']






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 16 '18 at 12:25









          schwobasegglschwobaseggl

          37.5k32443




          37.5k32443













          • thank you this works as desired, I would never figured that out

            – AlexW
            Nov 16 '18 at 12:30











          • is it possible to return the full dictionary for the matches?

            – AlexW
            Nov 16 '18 at 12:31













          • Yeah, just start the comprehension with [(d1['ifIndex'], v) for d1 ... instead of [v for d1 ...

            – schwobaseggl
            Nov 16 '18 at 12:34













          • sorry I changed it to the full dictonary is that one possible?

            – AlexW
            Nov 16 '18 at 12:37











          • Try and find out ;) (should work)

            – schwobaseggl
            Nov 16 '18 at 12:38



















          • thank you this works as desired, I would never figured that out

            – AlexW
            Nov 16 '18 at 12:30











          • is it possible to return the full dictionary for the matches?

            – AlexW
            Nov 16 '18 at 12:31













          • Yeah, just start the comprehension with [(d1['ifIndex'], v) for d1 ... instead of [v for d1 ...

            – schwobaseggl
            Nov 16 '18 at 12:34













          • sorry I changed it to the full dictonary is that one possible?

            – AlexW
            Nov 16 '18 at 12:37











          • Try and find out ;) (should work)

            – schwobaseggl
            Nov 16 '18 at 12:38

















          thank you this works as desired, I would never figured that out

          – AlexW
          Nov 16 '18 at 12:30





          thank you this works as desired, I would never figured that out

          – AlexW
          Nov 16 '18 at 12:30













          is it possible to return the full dictionary for the matches?

          – AlexW
          Nov 16 '18 at 12:31







          is it possible to return the full dictionary for the matches?

          – AlexW
          Nov 16 '18 at 12:31















          Yeah, just start the comprehension with [(d1['ifIndex'], v) for d1 ... instead of [v for d1 ...

          – schwobaseggl
          Nov 16 '18 at 12:34







          Yeah, just start the comprehension with [(d1['ifIndex'], v) for d1 ... instead of [v for d1 ...

          – schwobaseggl
          Nov 16 '18 at 12:34















          sorry I changed it to the full dictonary is that one possible?

          – AlexW
          Nov 16 '18 at 12:37





          sorry I changed it to the full dictonary is that one possible?

          – AlexW
          Nov 16 '18 at 12:37













          Try and find out ;) (should work)

          – schwobaseggl
          Nov 16 '18 at 12:38





          Try and find out ;) (should work)

          – schwobaseggl
          Nov 16 '18 at 12:38




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53337785%2fpython-comparing-two-dicts-values-for-contains-and-showing-matches%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

          Florida Star v. B. J. F.

          Danny Elfman

          Lugert, Oklahoma