How to return the values of dictionary key that has gained new values in a different dictionary












2















I have question a bit similar to:Replacing the value of a Python dictionary with the value of another dictionary that matches the other dictionaries key



However in my case I got two dictionaries



dict1 = {'foo' : ['val1' , 'val2' , 'val3'] , 'bar' : ['val4' , 'val5']}

dict2 = {'foo' : ['val2', 'val10', 'val11'] , 'bar' : ['val1' , 'val4']}


What I want to return is



dict3 = {'foo' : ['val10', 'val11'] , 'bar' : ['val1']}


and the opposite which is



dict4 = {'foo' : ['val1', 'val3'] , 'bar' : ['val5']}


where dict3 returns a dictionary of the values the keys 'foo' and 'bar' has gained in dict2 and the dict4 is a dictionary of the values the keys 'foo' and 'bar' has lost in dict2



One way I have tried solving this is to:



 iterate over both dictionaries then
if key of dict1 == key of dict2
return the values of the key in dict1 and compare with the values in dict2
return the values that aren't in both
as a dictionary of the key and those values


This idea isn't working and obviously highly inefficient. I was hoping there is a more efficient working way to do this










share|improve this question



























    2















    I have question a bit similar to:Replacing the value of a Python dictionary with the value of another dictionary that matches the other dictionaries key



    However in my case I got two dictionaries



    dict1 = {'foo' : ['val1' , 'val2' , 'val3'] , 'bar' : ['val4' , 'val5']}

    dict2 = {'foo' : ['val2', 'val10', 'val11'] , 'bar' : ['val1' , 'val4']}


    What I want to return is



    dict3 = {'foo' : ['val10', 'val11'] , 'bar' : ['val1']}


    and the opposite which is



    dict4 = {'foo' : ['val1', 'val3'] , 'bar' : ['val5']}


    where dict3 returns a dictionary of the values the keys 'foo' and 'bar' has gained in dict2 and the dict4 is a dictionary of the values the keys 'foo' and 'bar' has lost in dict2



    One way I have tried solving this is to:



     iterate over both dictionaries then
    if key of dict1 == key of dict2
    return the values of the key in dict1 and compare with the values in dict2
    return the values that aren't in both
    as a dictionary of the key and those values


    This idea isn't working and obviously highly inefficient. I was hoping there is a more efficient working way to do this










    share|improve this question

























      2












      2








      2








      I have question a bit similar to:Replacing the value of a Python dictionary with the value of another dictionary that matches the other dictionaries key



      However in my case I got two dictionaries



      dict1 = {'foo' : ['val1' , 'val2' , 'val3'] , 'bar' : ['val4' , 'val5']}

      dict2 = {'foo' : ['val2', 'val10', 'val11'] , 'bar' : ['val1' , 'val4']}


      What I want to return is



      dict3 = {'foo' : ['val10', 'val11'] , 'bar' : ['val1']}


      and the opposite which is



      dict4 = {'foo' : ['val1', 'val3'] , 'bar' : ['val5']}


      where dict3 returns a dictionary of the values the keys 'foo' and 'bar' has gained in dict2 and the dict4 is a dictionary of the values the keys 'foo' and 'bar' has lost in dict2



      One way I have tried solving this is to:



       iterate over both dictionaries then
      if key of dict1 == key of dict2
      return the values of the key in dict1 and compare with the values in dict2
      return the values that aren't in both
      as a dictionary of the key and those values


      This idea isn't working and obviously highly inefficient. I was hoping there is a more efficient working way to do this










      share|improve this question














      I have question a bit similar to:Replacing the value of a Python dictionary with the value of another dictionary that matches the other dictionaries key



      However in my case I got two dictionaries



      dict1 = {'foo' : ['val1' , 'val2' , 'val3'] , 'bar' : ['val4' , 'val5']}

      dict2 = {'foo' : ['val2', 'val10', 'val11'] , 'bar' : ['val1' , 'val4']}


      What I want to return is



      dict3 = {'foo' : ['val10', 'val11'] , 'bar' : ['val1']}


      and the opposite which is



      dict4 = {'foo' : ['val1', 'val3'] , 'bar' : ['val5']}


      where dict3 returns a dictionary of the values the keys 'foo' and 'bar' has gained in dict2 and the dict4 is a dictionary of the values the keys 'foo' and 'bar' has lost in dict2



      One way I have tried solving this is to:



       iterate over both dictionaries then
      if key of dict1 == key of dict2
      return the values of the key in dict1 and compare with the values in dict2
      return the values that aren't in both
      as a dictionary of the key and those values


      This idea isn't working and obviously highly inefficient. I was hoping there is a more efficient working way to do this







      python dictionary






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 15 '18 at 2:17









      MykelMykel

      336




      336
























          1 Answer
          1






          active

          oldest

          votes


















          3














          Two dict comprehensions will do the trick:



          dict3 = {k: [x for x in v if x not in dict1[k]] for k, v in dict2.items()}
          print(dict3)
          # {'foo': ['val10', 'val11'], 'bar': ['val1']}

          dict4 = {k: [x for x in v if x not in dict2[k]] for k, v in dict1.items()}
          print(dict4)
          # {'foo': ['val1', 'val3'], 'bar': ['val5']}


          The above two comprehensions basically filter out the values from key that don't exist in the other dictionary, which is done both ways.



          You can also do this without dict comprehensions:



          dict3 = {}
          for k,v in dict2.items():
          dict3[k] = [x for x in v if x not in dict1[k]]

          print(dict3)
          # {'foo': ['val10', 'val11'], 'bar': ['val1']}

          dict4 = {}
          for k,v in dict1.items():
          dict4[k] = [x for x in v if x not in dict2[k]]

          print(dict4)
          # {'foo': ['val1', 'val3'], 'bar': ['val5']}





          share|improve this answer


























          • Thanks. That was clear and worked. Is there a place I can learn more dict comprehensions?

            – Mykel
            Nov 15 '18 at 2:36








          • 1





            @Mykel You can try following www.datacamp.com/community/tutorials/python-dictionary-comprehension. This is a good tutorial on dictionaries in general, including dict comprehensions. Also note that you don't have to use dict comprehensions, they are just handy for making quick one liners. The above can be easily done without them, it just requires more code.

            – RoadRunner
            Nov 15 '18 at 2:38








          • 1





            I understand that other method requires more code. Dict comprehensions however are very handy and I am hoping to understand more of them. Thanks again

            – Mykel
            Nov 15 '18 at 2:47






          • 1





            @Mykel Yeah, they are very handy. If you understand list comprehensions, they are the same idea, just slightly different syntax. No problem man :).

            – RoadRunner
            Nov 15 '18 at 2:48











          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%2f53311495%2fhow-to-return-the-values-of-dictionary-key-that-has-gained-new-values-in-a-diffe%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









          3














          Two dict comprehensions will do the trick:



          dict3 = {k: [x for x in v if x not in dict1[k]] for k, v in dict2.items()}
          print(dict3)
          # {'foo': ['val10', 'val11'], 'bar': ['val1']}

          dict4 = {k: [x for x in v if x not in dict2[k]] for k, v in dict1.items()}
          print(dict4)
          # {'foo': ['val1', 'val3'], 'bar': ['val5']}


          The above two comprehensions basically filter out the values from key that don't exist in the other dictionary, which is done both ways.



          You can also do this without dict comprehensions:



          dict3 = {}
          for k,v in dict2.items():
          dict3[k] = [x for x in v if x not in dict1[k]]

          print(dict3)
          # {'foo': ['val10', 'val11'], 'bar': ['val1']}

          dict4 = {}
          for k,v in dict1.items():
          dict4[k] = [x for x in v if x not in dict2[k]]

          print(dict4)
          # {'foo': ['val1', 'val3'], 'bar': ['val5']}





          share|improve this answer


























          • Thanks. That was clear and worked. Is there a place I can learn more dict comprehensions?

            – Mykel
            Nov 15 '18 at 2:36








          • 1





            @Mykel You can try following www.datacamp.com/community/tutorials/python-dictionary-comprehension. This is a good tutorial on dictionaries in general, including dict comprehensions. Also note that you don't have to use dict comprehensions, they are just handy for making quick one liners. The above can be easily done without them, it just requires more code.

            – RoadRunner
            Nov 15 '18 at 2:38








          • 1





            I understand that other method requires more code. Dict comprehensions however are very handy and I am hoping to understand more of them. Thanks again

            – Mykel
            Nov 15 '18 at 2:47






          • 1





            @Mykel Yeah, they are very handy. If you understand list comprehensions, they are the same idea, just slightly different syntax. No problem man :).

            – RoadRunner
            Nov 15 '18 at 2:48
















          3














          Two dict comprehensions will do the trick:



          dict3 = {k: [x for x in v if x not in dict1[k]] for k, v in dict2.items()}
          print(dict3)
          # {'foo': ['val10', 'val11'], 'bar': ['val1']}

          dict4 = {k: [x for x in v if x not in dict2[k]] for k, v in dict1.items()}
          print(dict4)
          # {'foo': ['val1', 'val3'], 'bar': ['val5']}


          The above two comprehensions basically filter out the values from key that don't exist in the other dictionary, which is done both ways.



          You can also do this without dict comprehensions:



          dict3 = {}
          for k,v in dict2.items():
          dict3[k] = [x for x in v if x not in dict1[k]]

          print(dict3)
          # {'foo': ['val10', 'val11'], 'bar': ['val1']}

          dict4 = {}
          for k,v in dict1.items():
          dict4[k] = [x for x in v if x not in dict2[k]]

          print(dict4)
          # {'foo': ['val1', 'val3'], 'bar': ['val5']}





          share|improve this answer


























          • Thanks. That was clear and worked. Is there a place I can learn more dict comprehensions?

            – Mykel
            Nov 15 '18 at 2:36








          • 1





            @Mykel You can try following www.datacamp.com/community/tutorials/python-dictionary-comprehension. This is a good tutorial on dictionaries in general, including dict comprehensions. Also note that you don't have to use dict comprehensions, they are just handy for making quick one liners. The above can be easily done without them, it just requires more code.

            – RoadRunner
            Nov 15 '18 at 2:38








          • 1





            I understand that other method requires more code. Dict comprehensions however are very handy and I am hoping to understand more of them. Thanks again

            – Mykel
            Nov 15 '18 at 2:47






          • 1





            @Mykel Yeah, they are very handy. If you understand list comprehensions, they are the same idea, just slightly different syntax. No problem man :).

            – RoadRunner
            Nov 15 '18 at 2:48














          3












          3








          3







          Two dict comprehensions will do the trick:



          dict3 = {k: [x for x in v if x not in dict1[k]] for k, v in dict2.items()}
          print(dict3)
          # {'foo': ['val10', 'val11'], 'bar': ['val1']}

          dict4 = {k: [x for x in v if x not in dict2[k]] for k, v in dict1.items()}
          print(dict4)
          # {'foo': ['val1', 'val3'], 'bar': ['val5']}


          The above two comprehensions basically filter out the values from key that don't exist in the other dictionary, which is done both ways.



          You can also do this without dict comprehensions:



          dict3 = {}
          for k,v in dict2.items():
          dict3[k] = [x for x in v if x not in dict1[k]]

          print(dict3)
          # {'foo': ['val10', 'val11'], 'bar': ['val1']}

          dict4 = {}
          for k,v in dict1.items():
          dict4[k] = [x for x in v if x not in dict2[k]]

          print(dict4)
          # {'foo': ['val1', 'val3'], 'bar': ['val5']}





          share|improve this answer















          Two dict comprehensions will do the trick:



          dict3 = {k: [x for x in v if x not in dict1[k]] for k, v in dict2.items()}
          print(dict3)
          # {'foo': ['val10', 'val11'], 'bar': ['val1']}

          dict4 = {k: [x for x in v if x not in dict2[k]] for k, v in dict1.items()}
          print(dict4)
          # {'foo': ['val1', 'val3'], 'bar': ['val5']}


          The above two comprehensions basically filter out the values from key that don't exist in the other dictionary, which is done both ways.



          You can also do this without dict comprehensions:



          dict3 = {}
          for k,v in dict2.items():
          dict3[k] = [x for x in v if x not in dict1[k]]

          print(dict3)
          # {'foo': ['val10', 'val11'], 'bar': ['val1']}

          dict4 = {}
          for k,v in dict1.items():
          dict4[k] = [x for x in v if x not in dict2[k]]

          print(dict4)
          # {'foo': ['val1', 'val3'], 'bar': ['val5']}






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 15 '18 at 2:41

























          answered Nov 15 '18 at 2:24









          RoadRunnerRoadRunner

          11.3k31340




          11.3k31340













          • Thanks. That was clear and worked. Is there a place I can learn more dict comprehensions?

            – Mykel
            Nov 15 '18 at 2:36








          • 1





            @Mykel You can try following www.datacamp.com/community/tutorials/python-dictionary-comprehension. This is a good tutorial on dictionaries in general, including dict comprehensions. Also note that you don't have to use dict comprehensions, they are just handy for making quick one liners. The above can be easily done without them, it just requires more code.

            – RoadRunner
            Nov 15 '18 at 2:38








          • 1





            I understand that other method requires more code. Dict comprehensions however are very handy and I am hoping to understand more of them. Thanks again

            – Mykel
            Nov 15 '18 at 2:47






          • 1





            @Mykel Yeah, they are very handy. If you understand list comprehensions, they are the same idea, just slightly different syntax. No problem man :).

            – RoadRunner
            Nov 15 '18 at 2:48



















          • Thanks. That was clear and worked. Is there a place I can learn more dict comprehensions?

            – Mykel
            Nov 15 '18 at 2:36








          • 1





            @Mykel You can try following www.datacamp.com/community/tutorials/python-dictionary-comprehension. This is a good tutorial on dictionaries in general, including dict comprehensions. Also note that you don't have to use dict comprehensions, they are just handy for making quick one liners. The above can be easily done without them, it just requires more code.

            – RoadRunner
            Nov 15 '18 at 2:38








          • 1





            I understand that other method requires more code. Dict comprehensions however are very handy and I am hoping to understand more of them. Thanks again

            – Mykel
            Nov 15 '18 at 2:47






          • 1





            @Mykel Yeah, they are very handy. If you understand list comprehensions, they are the same idea, just slightly different syntax. No problem man :).

            – RoadRunner
            Nov 15 '18 at 2:48

















          Thanks. That was clear and worked. Is there a place I can learn more dict comprehensions?

          – Mykel
          Nov 15 '18 at 2:36







          Thanks. That was clear and worked. Is there a place I can learn more dict comprehensions?

          – Mykel
          Nov 15 '18 at 2:36






          1




          1





          @Mykel You can try following www.datacamp.com/community/tutorials/python-dictionary-comprehension. This is a good tutorial on dictionaries in general, including dict comprehensions. Also note that you don't have to use dict comprehensions, they are just handy for making quick one liners. The above can be easily done without them, it just requires more code.

          – RoadRunner
          Nov 15 '18 at 2:38







          @Mykel You can try following www.datacamp.com/community/tutorials/python-dictionary-comprehension. This is a good tutorial on dictionaries in general, including dict comprehensions. Also note that you don't have to use dict comprehensions, they are just handy for making quick one liners. The above can be easily done without them, it just requires more code.

          – RoadRunner
          Nov 15 '18 at 2:38






          1




          1





          I understand that other method requires more code. Dict comprehensions however are very handy and I am hoping to understand more of them. Thanks again

          – Mykel
          Nov 15 '18 at 2:47





          I understand that other method requires more code. Dict comprehensions however are very handy and I am hoping to understand more of them. Thanks again

          – Mykel
          Nov 15 '18 at 2:47




          1




          1





          @Mykel Yeah, they are very handy. If you understand list comprehensions, they are the same idea, just slightly different syntax. No problem man :).

          – RoadRunner
          Nov 15 '18 at 2:48





          @Mykel Yeah, they are very handy. If you understand list comprehensions, they are the same idea, just slightly different syntax. No problem man :).

          – RoadRunner
          Nov 15 '18 at 2:48




















          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%2f53311495%2fhow-to-return-the-values-of-dictionary-key-that-has-gained-new-values-in-a-diffe%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

          Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues