Merge two dicts by same key












14














I have the following two toy dicts



d1 = {
'a': [2,4,5,6,8,10],
'b': [1,2,5,6,9,12],
'c': [0,4,5,8,10,21]
}
d2 = {
'a': [12,15],
'b': [14,16],
'c': [23,35]
}


and I would like get a unique dictionary where I stack the second dictionary values after the first ones, within the same square brackets.



I tried the following code



d_comb = {key:[d1[key], d2[key]] for key in d1}


but the output I obtain has two lists within a list for each key, i.e.



{'a': [[2, 4, 5, 6, 8, 10], [12, 15]],
'b': [[1, 2, 5, 6, 9, 12], [14, 16]],
'c': [[0, 4, 5, 8, 10, 21], [23, 35]]}


whereas I would like to obtain



{'a': [2, 4, 5, 6, 8, 10, 12, 15],
'b': [1, 2, 5, 6, 9, 12, 14, 16],
'c': [0, 4, 5, 8, 10, 21, 23, 35]}


How can I do that with a line or two of code?










share|improve this question
























  • Are we sure that both d1 and d2 have same set of keys?
    – cph_sto
    4 hours ago










  • In this example and according to my present needs, yes. Of course I'm sure that with different sets of keys in the two dictionaries the code will be different.
    – Ric S
    3 hours ago
















14














I have the following two toy dicts



d1 = {
'a': [2,4,5,6,8,10],
'b': [1,2,5,6,9,12],
'c': [0,4,5,8,10,21]
}
d2 = {
'a': [12,15],
'b': [14,16],
'c': [23,35]
}


and I would like get a unique dictionary where I stack the second dictionary values after the first ones, within the same square brackets.



I tried the following code



d_comb = {key:[d1[key], d2[key]] for key in d1}


but the output I obtain has two lists within a list for each key, i.e.



{'a': [[2, 4, 5, 6, 8, 10], [12, 15]],
'b': [[1, 2, 5, 6, 9, 12], [14, 16]],
'c': [[0, 4, 5, 8, 10, 21], [23, 35]]}


whereas I would like to obtain



{'a': [2, 4, 5, 6, 8, 10, 12, 15],
'b': [1, 2, 5, 6, 9, 12, 14, 16],
'c': [0, 4, 5, 8, 10, 21, 23, 35]}


How can I do that with a line or two of code?










share|improve this question
























  • Are we sure that both d1 and d2 have same set of keys?
    – cph_sto
    4 hours ago










  • In this example and according to my present needs, yes. Of course I'm sure that with different sets of keys in the two dictionaries the code will be different.
    – Ric S
    3 hours ago














14












14








14







I have the following two toy dicts



d1 = {
'a': [2,4,5,6,8,10],
'b': [1,2,5,6,9,12],
'c': [0,4,5,8,10,21]
}
d2 = {
'a': [12,15],
'b': [14,16],
'c': [23,35]
}


and I would like get a unique dictionary where I stack the second dictionary values after the first ones, within the same square brackets.



I tried the following code



d_comb = {key:[d1[key], d2[key]] for key in d1}


but the output I obtain has two lists within a list for each key, i.e.



{'a': [[2, 4, 5, 6, 8, 10], [12, 15]],
'b': [[1, 2, 5, 6, 9, 12], [14, 16]],
'c': [[0, 4, 5, 8, 10, 21], [23, 35]]}


whereas I would like to obtain



{'a': [2, 4, 5, 6, 8, 10, 12, 15],
'b': [1, 2, 5, 6, 9, 12, 14, 16],
'c': [0, 4, 5, 8, 10, 21, 23, 35]}


How can I do that with a line or two of code?










share|improve this question















I have the following two toy dicts



d1 = {
'a': [2,4,5,6,8,10],
'b': [1,2,5,6,9,12],
'c': [0,4,5,8,10,21]
}
d2 = {
'a': [12,15],
'b': [14,16],
'c': [23,35]
}


and I would like get a unique dictionary where I stack the second dictionary values after the first ones, within the same square brackets.



I tried the following code



d_comb = {key:[d1[key], d2[key]] for key in d1}


but the output I obtain has two lists within a list for each key, i.e.



{'a': [[2, 4, 5, 6, 8, 10], [12, 15]],
'b': [[1, 2, 5, 6, 9, 12], [14, 16]],
'c': [[0, 4, 5, 8, 10, 21], [23, 35]]}


whereas I would like to obtain



{'a': [2, 4, 5, 6, 8, 10, 12, 15],
'b': [1, 2, 5, 6, 9, 12, 14, 16],
'c': [0, 4, 5, 8, 10, 21, 23, 35]}


How can I do that with a line or two of code?







python list dictionary






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 3 hours ago









yatu

5,8201524




5,8201524










asked 5 hours ago









Ric SRic S

335211




335211












  • Are we sure that both d1 and d2 have same set of keys?
    – cph_sto
    4 hours ago










  • In this example and according to my present needs, yes. Of course I'm sure that with different sets of keys in the two dictionaries the code will be different.
    – Ric S
    3 hours ago


















  • Are we sure that both d1 and d2 have same set of keys?
    – cph_sto
    4 hours ago










  • In this example and according to my present needs, yes. Of course I'm sure that with different sets of keys in the two dictionaries the code will be different.
    – Ric S
    3 hours ago
















Are we sure that both d1 and d2 have same set of keys?
– cph_sto
4 hours ago




Are we sure that both d1 and d2 have same set of keys?
– cph_sto
4 hours ago












In this example and according to my present needs, yes. Of course I'm sure that with different sets of keys in the two dictionaries the code will be different.
– Ric S
3 hours ago




In this example and according to my present needs, yes. Of course I'm sure that with different sets of keys in the two dictionaries the code will be different.
– Ric S
3 hours ago












5 Answers
5






active

oldest

votes


















18














You almost had it, instead use + to append both lists:



{key: d1[key] + d2[key] for key in d1}

{'a': [2, 4, 5, 6, 8, 10, 12, 15],
'b': [1, 2, 5, 6, 9, 12, 14, 16],
'c': [0, 4, 5, 8, 10, 21, 23, 35]}





share|improve this answer

















  • 1




    Damn, it was the plus sign, not the comma.. I knew it was easy but it didn't come to mind! Thanks
    – Ric S
    5 hours ago





















6














You could use extended iterable unpacking:



d1 = {
'a': [2,4,5,6,8,10],
'b': [1,2,5,6,9,12],
'c': [0,4,5,8,10,21]
}
d2 = {
'a': [12,15],
'b': [14,16],
'c': [23,35]
}

d_comb = {key:[*d1[key], *d2[key]] for key in d1}

print(d_comb)


Output



{'c': [0, 4, 5, 8, 10, 21, 23, 35], 'b': [1, 2, 5, 6, 9, 12, 14, 16], 'a': [2, 4, 5, 6, 8, 10, 12, 15]}





share|improve this answer





























    5














    if not all the keys from d2 are in d1, then the simplest thing is using set union and dict.get:



    combined_keys = d1.keys() | d2.keys()
    d_comb = {key: d1.get(key, ) + d2.get(key, ) for key in combined_keys}





    share|improve this answer





















    • Nice solution indeed.
      – cph_sto
      2 hours ago



















    2














    The code will work irrespective of whether d1 or d2 have the same set of keys. I have added a key 'e' in d1 and 'd' in d2.



    d1 = {'a': [2, 4, 5, 6, 8, 10], 'b': [1, 2, 5, 6, 9, 12], 'c': [0, 4, 5, 8, 10, 21], 'e':[0,0,0]}
    d2 = {'a': [12, 15], 'b': [14, 16], 'c': [23, 35], 'd': [13, 3]}

    d2_keys_not_in_d1 = d2.keys() - d1.keys()
    d1_keys_not_in_d2 = d1.keys() - d2.keys()
    common_keys = d2.keys() & d1.keys()

    for i in common_keys:
    d[i]=d1[i]+d2[i]
    for i in d1_keys_not_in_d2:
    d[i]=d1[i]
    for i in d2_keys_not_in_d1:
    d[i]=d2[i]
    d
    {'a': [2, 4, 5, 6, 8, 10, 12, 15],
    'b': [1, 2, 5, 6, 9, 12, 14, 16],
    'c': [0, 4, 5, 8, 10, 21, 23, 35],
    'd': [13, 3],
    'e': [0, 0, 0]}





    share|improve this answer























    • this fails if there are keys in d1 that are not in d2, and d2_keys_not_in_d1 can be expressed simpler as d2.keys() - d1.keys()
      – Maarten Fabré
      3 hours ago










    • Thanks Maarten for your feedback. Very appreciated. I have changed the code accordingly and used your expression, which was lot more succinct never the less.
      – cph_sto
      2 hours ago










    • the common keys can be expressed as ` d2.keys() & d1.keys()`
      – Maarten Fabré
      2 hours ago










    • Thanks a lot Maarten. Very helpful. I have learnt something :)
      – cph_sto
      2 hours ago



















    0














    You can use itertools.chain to efficiently construct a single list from input lists:



    from itertools import chain
    d_comb = {key: list(chain(d1[key], d2[key])) for key in d1}


    For the more general case covering an arbitrary number of dictionaries and keys which are not equal across dictionaries, see Merging dictionary value lists in python.






    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',
      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%2f54109167%2fmerge-two-dicts-by-same-key%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      5 Answers
      5






      active

      oldest

      votes








      5 Answers
      5






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      18














      You almost had it, instead use + to append both lists:



      {key: d1[key] + d2[key] for key in d1}

      {'a': [2, 4, 5, 6, 8, 10, 12, 15],
      'b': [1, 2, 5, 6, 9, 12, 14, 16],
      'c': [0, 4, 5, 8, 10, 21, 23, 35]}





      share|improve this answer

















      • 1




        Damn, it was the plus sign, not the comma.. I knew it was easy but it didn't come to mind! Thanks
        – Ric S
        5 hours ago


















      18














      You almost had it, instead use + to append both lists:



      {key: d1[key] + d2[key] for key in d1}

      {'a': [2, 4, 5, 6, 8, 10, 12, 15],
      'b': [1, 2, 5, 6, 9, 12, 14, 16],
      'c': [0, 4, 5, 8, 10, 21, 23, 35]}





      share|improve this answer

















      • 1




        Damn, it was the plus sign, not the comma.. I knew it was easy but it didn't come to mind! Thanks
        – Ric S
        5 hours ago
















      18












      18








      18






      You almost had it, instead use + to append both lists:



      {key: d1[key] + d2[key] for key in d1}

      {'a': [2, 4, 5, 6, 8, 10, 12, 15],
      'b': [1, 2, 5, 6, 9, 12, 14, 16],
      'c': [0, 4, 5, 8, 10, 21, 23, 35]}





      share|improve this answer












      You almost had it, instead use + to append both lists:



      {key: d1[key] + d2[key] for key in d1}

      {'a': [2, 4, 5, 6, 8, 10, 12, 15],
      'b': [1, 2, 5, 6, 9, 12, 14, 16],
      'c': [0, 4, 5, 8, 10, 21, 23, 35]}






      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered 5 hours ago









      yatuyatu

      5,8201524




      5,8201524








      • 1




        Damn, it was the plus sign, not the comma.. I knew it was easy but it didn't come to mind! Thanks
        – Ric S
        5 hours ago
















      • 1




        Damn, it was the plus sign, not the comma.. I knew it was easy but it didn't come to mind! Thanks
        – Ric S
        5 hours ago










      1




      1




      Damn, it was the plus sign, not the comma.. I knew it was easy but it didn't come to mind! Thanks
      – Ric S
      5 hours ago






      Damn, it was the plus sign, not the comma.. I knew it was easy but it didn't come to mind! Thanks
      – Ric S
      5 hours ago















      6














      You could use extended iterable unpacking:



      d1 = {
      'a': [2,4,5,6,8,10],
      'b': [1,2,5,6,9,12],
      'c': [0,4,5,8,10,21]
      }
      d2 = {
      'a': [12,15],
      'b': [14,16],
      'c': [23,35]
      }

      d_comb = {key:[*d1[key], *d2[key]] for key in d1}

      print(d_comb)


      Output



      {'c': [0, 4, 5, 8, 10, 21, 23, 35], 'b': [1, 2, 5, 6, 9, 12, 14, 16], 'a': [2, 4, 5, 6, 8, 10, 12, 15]}





      share|improve this answer


























        6














        You could use extended iterable unpacking:



        d1 = {
        'a': [2,4,5,6,8,10],
        'b': [1,2,5,6,9,12],
        'c': [0,4,5,8,10,21]
        }
        d2 = {
        'a': [12,15],
        'b': [14,16],
        'c': [23,35]
        }

        d_comb = {key:[*d1[key], *d2[key]] for key in d1}

        print(d_comb)


        Output



        {'c': [0, 4, 5, 8, 10, 21, 23, 35], 'b': [1, 2, 5, 6, 9, 12, 14, 16], 'a': [2, 4, 5, 6, 8, 10, 12, 15]}





        share|improve this answer
























          6












          6








          6






          You could use extended iterable unpacking:



          d1 = {
          'a': [2,4,5,6,8,10],
          'b': [1,2,5,6,9,12],
          'c': [0,4,5,8,10,21]
          }
          d2 = {
          'a': [12,15],
          'b': [14,16],
          'c': [23,35]
          }

          d_comb = {key:[*d1[key], *d2[key]] for key in d1}

          print(d_comb)


          Output



          {'c': [0, 4, 5, 8, 10, 21, 23, 35], 'b': [1, 2, 5, 6, 9, 12, 14, 16], 'a': [2, 4, 5, 6, 8, 10, 12, 15]}





          share|improve this answer












          You could use extended iterable unpacking:



          d1 = {
          'a': [2,4,5,6,8,10],
          'b': [1,2,5,6,9,12],
          'c': [0,4,5,8,10,21]
          }
          d2 = {
          'a': [12,15],
          'b': [14,16],
          'c': [23,35]
          }

          d_comb = {key:[*d1[key], *d2[key]] for key in d1}

          print(d_comb)


          Output



          {'c': [0, 4, 5, 8, 10, 21, 23, 35], 'b': [1, 2, 5, 6, 9, 12, 14, 16], 'a': [2, 4, 5, 6, 8, 10, 12, 15]}






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 5 hours ago









          Daniel MesejoDaniel Mesejo

          14.9k21028




          14.9k21028























              5














              if not all the keys from d2 are in d1, then the simplest thing is using set union and dict.get:



              combined_keys = d1.keys() | d2.keys()
              d_comb = {key: d1.get(key, ) + d2.get(key, ) for key in combined_keys}





              share|improve this answer





















              • Nice solution indeed.
                – cph_sto
                2 hours ago
















              5














              if not all the keys from d2 are in d1, then the simplest thing is using set union and dict.get:



              combined_keys = d1.keys() | d2.keys()
              d_comb = {key: d1.get(key, ) + d2.get(key, ) for key in combined_keys}





              share|improve this answer





















              • Nice solution indeed.
                – cph_sto
                2 hours ago














              5












              5








              5






              if not all the keys from d2 are in d1, then the simplest thing is using set union and dict.get:



              combined_keys = d1.keys() | d2.keys()
              d_comb = {key: d1.get(key, ) + d2.get(key, ) for key in combined_keys}





              share|improve this answer












              if not all the keys from d2 are in d1, then the simplest thing is using set union and dict.get:



              combined_keys = d1.keys() | d2.keys()
              d_comb = {key: d1.get(key, ) + d2.get(key, ) for key in combined_keys}






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered 3 hours ago









              Maarten FabréMaarten Fabré

              4,8851821




              4,8851821












              • Nice solution indeed.
                – cph_sto
                2 hours ago


















              • Nice solution indeed.
                – cph_sto
                2 hours ago
















              Nice solution indeed.
              – cph_sto
              2 hours ago




              Nice solution indeed.
              – cph_sto
              2 hours ago











              2














              The code will work irrespective of whether d1 or d2 have the same set of keys. I have added a key 'e' in d1 and 'd' in d2.



              d1 = {'a': [2, 4, 5, 6, 8, 10], 'b': [1, 2, 5, 6, 9, 12], 'c': [0, 4, 5, 8, 10, 21], 'e':[0,0,0]}
              d2 = {'a': [12, 15], 'b': [14, 16], 'c': [23, 35], 'd': [13, 3]}

              d2_keys_not_in_d1 = d2.keys() - d1.keys()
              d1_keys_not_in_d2 = d1.keys() - d2.keys()
              common_keys = d2.keys() & d1.keys()

              for i in common_keys:
              d[i]=d1[i]+d2[i]
              for i in d1_keys_not_in_d2:
              d[i]=d1[i]
              for i in d2_keys_not_in_d1:
              d[i]=d2[i]
              d
              {'a': [2, 4, 5, 6, 8, 10, 12, 15],
              'b': [1, 2, 5, 6, 9, 12, 14, 16],
              'c': [0, 4, 5, 8, 10, 21, 23, 35],
              'd': [13, 3],
              'e': [0, 0, 0]}





              share|improve this answer























              • this fails if there are keys in d1 that are not in d2, and d2_keys_not_in_d1 can be expressed simpler as d2.keys() - d1.keys()
                – Maarten Fabré
                3 hours ago










              • Thanks Maarten for your feedback. Very appreciated. I have changed the code accordingly and used your expression, which was lot more succinct never the less.
                – cph_sto
                2 hours ago










              • the common keys can be expressed as ` d2.keys() & d1.keys()`
                – Maarten Fabré
                2 hours ago










              • Thanks a lot Maarten. Very helpful. I have learnt something :)
                – cph_sto
                2 hours ago
















              2














              The code will work irrespective of whether d1 or d2 have the same set of keys. I have added a key 'e' in d1 and 'd' in d2.



              d1 = {'a': [2, 4, 5, 6, 8, 10], 'b': [1, 2, 5, 6, 9, 12], 'c': [0, 4, 5, 8, 10, 21], 'e':[0,0,0]}
              d2 = {'a': [12, 15], 'b': [14, 16], 'c': [23, 35], 'd': [13, 3]}

              d2_keys_not_in_d1 = d2.keys() - d1.keys()
              d1_keys_not_in_d2 = d1.keys() - d2.keys()
              common_keys = d2.keys() & d1.keys()

              for i in common_keys:
              d[i]=d1[i]+d2[i]
              for i in d1_keys_not_in_d2:
              d[i]=d1[i]
              for i in d2_keys_not_in_d1:
              d[i]=d2[i]
              d
              {'a': [2, 4, 5, 6, 8, 10, 12, 15],
              'b': [1, 2, 5, 6, 9, 12, 14, 16],
              'c': [0, 4, 5, 8, 10, 21, 23, 35],
              'd': [13, 3],
              'e': [0, 0, 0]}





              share|improve this answer























              • this fails if there are keys in d1 that are not in d2, and d2_keys_not_in_d1 can be expressed simpler as d2.keys() - d1.keys()
                – Maarten Fabré
                3 hours ago










              • Thanks Maarten for your feedback. Very appreciated. I have changed the code accordingly and used your expression, which was lot more succinct never the less.
                – cph_sto
                2 hours ago










              • the common keys can be expressed as ` d2.keys() & d1.keys()`
                – Maarten Fabré
                2 hours ago










              • Thanks a lot Maarten. Very helpful. I have learnt something :)
                – cph_sto
                2 hours ago














              2












              2








              2






              The code will work irrespective of whether d1 or d2 have the same set of keys. I have added a key 'e' in d1 and 'd' in d2.



              d1 = {'a': [2, 4, 5, 6, 8, 10], 'b': [1, 2, 5, 6, 9, 12], 'c': [0, 4, 5, 8, 10, 21], 'e':[0,0,0]}
              d2 = {'a': [12, 15], 'b': [14, 16], 'c': [23, 35], 'd': [13, 3]}

              d2_keys_not_in_d1 = d2.keys() - d1.keys()
              d1_keys_not_in_d2 = d1.keys() - d2.keys()
              common_keys = d2.keys() & d1.keys()

              for i in common_keys:
              d[i]=d1[i]+d2[i]
              for i in d1_keys_not_in_d2:
              d[i]=d1[i]
              for i in d2_keys_not_in_d1:
              d[i]=d2[i]
              d
              {'a': [2, 4, 5, 6, 8, 10, 12, 15],
              'b': [1, 2, 5, 6, 9, 12, 14, 16],
              'c': [0, 4, 5, 8, 10, 21, 23, 35],
              'd': [13, 3],
              'e': [0, 0, 0]}





              share|improve this answer














              The code will work irrespective of whether d1 or d2 have the same set of keys. I have added a key 'e' in d1 and 'd' in d2.



              d1 = {'a': [2, 4, 5, 6, 8, 10], 'b': [1, 2, 5, 6, 9, 12], 'c': [0, 4, 5, 8, 10, 21], 'e':[0,0,0]}
              d2 = {'a': [12, 15], 'b': [14, 16], 'c': [23, 35], 'd': [13, 3]}

              d2_keys_not_in_d1 = d2.keys() - d1.keys()
              d1_keys_not_in_d2 = d1.keys() - d2.keys()
              common_keys = d2.keys() & d1.keys()

              for i in common_keys:
              d[i]=d1[i]+d2[i]
              for i in d1_keys_not_in_d2:
              d[i]=d1[i]
              for i in d2_keys_not_in_d1:
              d[i]=d2[i]
              d
              {'a': [2, 4, 5, 6, 8, 10, 12, 15],
              'b': [1, 2, 5, 6, 9, 12, 14, 16],
              'c': [0, 4, 5, 8, 10, 21, 23, 35],
              'd': [13, 3],
              'e': [0, 0, 0]}






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited 2 hours ago

























              answered 4 hours ago









              cph_stocph_sto

              1,352219




              1,352219












              • this fails if there are keys in d1 that are not in d2, and d2_keys_not_in_d1 can be expressed simpler as d2.keys() - d1.keys()
                – Maarten Fabré
                3 hours ago










              • Thanks Maarten for your feedback. Very appreciated. I have changed the code accordingly and used your expression, which was lot more succinct never the less.
                – cph_sto
                2 hours ago










              • the common keys can be expressed as ` d2.keys() & d1.keys()`
                – Maarten Fabré
                2 hours ago










              • Thanks a lot Maarten. Very helpful. I have learnt something :)
                – cph_sto
                2 hours ago


















              • this fails if there are keys in d1 that are not in d2, and d2_keys_not_in_d1 can be expressed simpler as d2.keys() - d1.keys()
                – Maarten Fabré
                3 hours ago










              • Thanks Maarten for your feedback. Very appreciated. I have changed the code accordingly and used your expression, which was lot more succinct never the less.
                – cph_sto
                2 hours ago










              • the common keys can be expressed as ` d2.keys() & d1.keys()`
                – Maarten Fabré
                2 hours ago










              • Thanks a lot Maarten. Very helpful. I have learnt something :)
                – cph_sto
                2 hours ago
















              this fails if there are keys in d1 that are not in d2, and d2_keys_not_in_d1 can be expressed simpler as d2.keys() - d1.keys()
              – Maarten Fabré
              3 hours ago




              this fails if there are keys in d1 that are not in d2, and d2_keys_not_in_d1 can be expressed simpler as d2.keys() - d1.keys()
              – Maarten Fabré
              3 hours ago












              Thanks Maarten for your feedback. Very appreciated. I have changed the code accordingly and used your expression, which was lot more succinct never the less.
              – cph_sto
              2 hours ago




              Thanks Maarten for your feedback. Very appreciated. I have changed the code accordingly and used your expression, which was lot more succinct never the less.
              – cph_sto
              2 hours ago












              the common keys can be expressed as ` d2.keys() & d1.keys()`
              – Maarten Fabré
              2 hours ago




              the common keys can be expressed as ` d2.keys() & d1.keys()`
              – Maarten Fabré
              2 hours ago












              Thanks a lot Maarten. Very helpful. I have learnt something :)
              – cph_sto
              2 hours ago




              Thanks a lot Maarten. Very helpful. I have learnt something :)
              – cph_sto
              2 hours ago











              0














              You can use itertools.chain to efficiently construct a single list from input lists:



              from itertools import chain
              d_comb = {key: list(chain(d1[key], d2[key])) for key in d1}


              For the more general case covering an arbitrary number of dictionaries and keys which are not equal across dictionaries, see Merging dictionary value lists in python.






              share|improve this answer


























                0














                You can use itertools.chain to efficiently construct a single list from input lists:



                from itertools import chain
                d_comb = {key: list(chain(d1[key], d2[key])) for key in d1}


                For the more general case covering an arbitrary number of dictionaries and keys which are not equal across dictionaries, see Merging dictionary value lists in python.






                share|improve this answer
























                  0












                  0








                  0






                  You can use itertools.chain to efficiently construct a single list from input lists:



                  from itertools import chain
                  d_comb = {key: list(chain(d1[key], d2[key])) for key in d1}


                  For the more general case covering an arbitrary number of dictionaries and keys which are not equal across dictionaries, see Merging dictionary value lists in python.






                  share|improve this answer












                  You can use itertools.chain to efficiently construct a single list from input lists:



                  from itertools import chain
                  d_comb = {key: list(chain(d1[key], d2[key])) for key in d1}


                  For the more general case covering an arbitrary number of dictionaries and keys which are not equal across dictionaries, see Merging dictionary value lists in python.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 5 hours ago









                  jppjpp

                  93.5k2054104




                  93.5k2054104






























                      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.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • 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%2f54109167%2fmerge-two-dicts-by-same-key%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

                      Liste der Baudenkmale in Friedland (Mecklenburg)

                      Single-Malt-Whisky

                      Czorneboh