Merge two dicts by same key [duplicate]





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







25
















This question already has an answer here:




  • Merging dictionary value lists in python

    3 answers




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















marked as duplicate by coldspeed python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 9 at 18:21


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • Are we sure that both d1 and d2 have same set of keys?

    – cph_sto
    Jan 9 at 12:39











  • 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
    Jan 9 at 13:03


















25
















This question already has an answer here:




  • Merging dictionary value lists in python

    3 answers




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















marked as duplicate by coldspeed python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 9 at 18:21


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • Are we sure that both d1 and d2 have same set of keys?

    – cph_sto
    Jan 9 at 12:39











  • 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
    Jan 9 at 13:03














25












25








25









This question already has an answer here:




  • Merging dictionary value lists in python

    3 answers




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

















This question already has an answer here:




  • Merging dictionary value lists in python

    3 answers




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?





This question already has an answer here:




  • Merging dictionary value lists in python

    3 answers








python list dictionary






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 9 at 13:25









yatu

15.6k41542




15.6k41542










asked Jan 9 at 11:32









Ric SRic S

703420




703420




marked as duplicate by coldspeed python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 9 at 18:21


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by coldspeed python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 9 at 18:21


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • Are we sure that both d1 and d2 have same set of keys?

    – cph_sto
    Jan 9 at 12:39











  • 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
    Jan 9 at 13:03



















  • Are we sure that both d1 and d2 have same set of keys?

    – cph_sto
    Jan 9 at 12:39











  • 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
    Jan 9 at 13:03

















Are we sure that both d1 and d2 have same set of keys?

– cph_sto
Jan 9 at 12:39





Are we sure that both d1 and d2 have same set of keys?

– cph_sto
Jan 9 at 12:39













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
Jan 9 at 13:03





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
Jan 9 at 13:03












5 Answers
5






active

oldest

votes


















30














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



















  • 2





    Damn, it was the plus sign, not the comma.. I knew it was easy but it didn't come to mind! Thanks

    – Ric S
    Jan 9 at 11:38





















12














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































    9














    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































      4














      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é
        Jan 9 at 13:31











      • 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
        Jan 9 at 14:02











      • the common keys can be expressed as ` d2.keys() & d1.keys()`

        – Maarten Fabré
        Jan 9 at 14:14











      • Thanks a lot Maarten. Very helpful. I have learnt something :)

        – cph_sto
        Jan 9 at 14:16



















      3














      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






























        5 Answers
        5






        active

        oldest

        votes








        5 Answers
        5






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        30














        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



















        • 2





          Damn, it was the plus sign, not the comma.. I knew it was easy but it didn't come to mind! Thanks

          – Ric S
          Jan 9 at 11:38


















        30














        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



















        • 2





          Damn, it was the plus sign, not the comma.. I knew it was easy but it didn't come to mind! Thanks

          – Ric S
          Jan 9 at 11:38
















        30












        30








        30







        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 Jan 9 at 11:35









        yatuyatu

        15.6k41542




        15.6k41542








        • 2





          Damn, it was the plus sign, not the comma.. I knew it was easy but it didn't come to mind! Thanks

          – Ric S
          Jan 9 at 11:38
















        • 2





          Damn, it was the plus sign, not the comma.. I knew it was easy but it didn't come to mind! Thanks

          – Ric S
          Jan 9 at 11:38










        2




        2





        Damn, it was the plus sign, not the comma.. I knew it was easy but it didn't come to mind! Thanks

        – Ric S
        Jan 9 at 11:38







        Damn, it was the plus sign, not the comma.. I knew it was easy but it didn't come to mind! Thanks

        – Ric S
        Jan 9 at 11:38















        12














        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




























          12














          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


























            12












            12








            12







            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 Jan 9 at 13:29









            Maarten FabréMaarten Fabré

            5,1501824




            5,1501824























                9














                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




























                  9














                  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


























                    9












                    9








                    9







                    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 Jan 9 at 11:38









                    Daniel MesejoDaniel Mesejo

                    18.8k21533




                    18.8k21533























                        4














                        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é
                          Jan 9 at 13:31











                        • 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
                          Jan 9 at 14:02











                        • the common keys can be expressed as ` d2.keys() & d1.keys()`

                          – Maarten Fabré
                          Jan 9 at 14:14











                        • Thanks a lot Maarten. Very helpful. I have learnt something :)

                          – cph_sto
                          Jan 9 at 14:16
















                        4














                        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é
                          Jan 9 at 13:31











                        • 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
                          Jan 9 at 14:02











                        • the common keys can be expressed as ` d2.keys() & d1.keys()`

                          – Maarten Fabré
                          Jan 9 at 14:14











                        • Thanks a lot Maarten. Very helpful. I have learnt something :)

                          – cph_sto
                          Jan 9 at 14:16














                        4












                        4








                        4







                        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 Jan 9 at 14:15

























                        answered Jan 9 at 11:57









                        cph_stocph_sto

                        2,4832523




                        2,4832523













                        • 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é
                          Jan 9 at 13:31











                        • 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
                          Jan 9 at 14:02











                        • the common keys can be expressed as ` d2.keys() & d1.keys()`

                          – Maarten Fabré
                          Jan 9 at 14:14











                        • Thanks a lot Maarten. Very helpful. I have learnt something :)

                          – cph_sto
                          Jan 9 at 14:16



















                        • 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é
                          Jan 9 at 13:31











                        • 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
                          Jan 9 at 14:02











                        • the common keys can be expressed as ` d2.keys() & d1.keys()`

                          – Maarten Fabré
                          Jan 9 at 14:14











                        • Thanks a lot Maarten. Very helpful. I have learnt something :)

                          – cph_sto
                          Jan 9 at 14:16

















                        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é
                        Jan 9 at 13:31





                        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é
                        Jan 9 at 13:31













                        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
                        Jan 9 at 14:02





                        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
                        Jan 9 at 14:02













                        the common keys can be expressed as ` d2.keys() & d1.keys()`

                        – Maarten Fabré
                        Jan 9 at 14:14





                        the common keys can be expressed as ` d2.keys() & d1.keys()`

                        – Maarten Fabré
                        Jan 9 at 14:14













                        Thanks a lot Maarten. Very helpful. I have learnt something :)

                        – cph_sto
                        Jan 9 at 14:16





                        Thanks a lot Maarten. Very helpful. I have learnt something :)

                        – cph_sto
                        Jan 9 at 14:16











                        3














                        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




























                          3














                          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


























                            3












                            3








                            3







                            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 Jan 9 at 11:36









                            jppjpp

                            102k2166116




                            102k2166116















                                Popular posts from this blog

                                Bressuire

                                Cabo Verde

                                Gyllenstierna