Sum values in a Python list of dictionaries using a common key/value












3















Apologies if this question has already been asked. I'm a newbie at this but I've looked at a couple of other questions/answers that appear similar to mine but can't find one that does the trick. I've tried Counter but can't seem to figure out how to keep the ID key/value.



I'm trying to sum the values in a Python dictionary using a common key/value. E.g.,



list = [
{'ID':1, 'T2':10, 'T3':20},
{'ID':2, 'T2':5, 'T3':0},
{'ID':1, 'T2':5, 'T3':10},
{'ID':2, 'T2':10, 'T3':30},
{'ID':3, 'T2':5, 'T3':0}
]


but I need this:



newlist = [
{'ID':1, 'T2':15, 'T3':30},
{'ID':2, 'T2':15, 'T3':30},
{'ID':3, 'T2':5, 'T3':0}
]


Any help appreciated.



UPDATE:



I have borrowed from another answer and tried this:



superdict = {}
for d in rows:
for k, v in d.items():
if superdict.get(k) is None:
superdict[k] =
if superdict.get(k) is not None:
superdict[k].append(v)


But instead of keeping getting a new list of combined/added values, I get something like this:



'ID': ['3903', '3997', '3997', '3997', '3947', '4097', '3445', 
'3997', '4107', '3997', '3445', '3997', '3997', '3997',
'3997', '3445', '3997', etc. etc.


UPDATE 2:



Sorry for the confusion in regard to my examples. What I'm looking for is a way to keep the ID value static but add the other values in the dictionary. So all of the values in corresponding to IDs with a value of 1 would be added together.



E.g. this:



list = [{'ID':1, 'T2':10, 'T3':20}, {'ID':1, 'T2':5, 'T3':10}]


Becomes this:



newlist = [{'ID':1, 'T2':15, 'T3':30}]


Hope this helps.










share|improve this question

























  • What is going on in your logic?

    – squiguy
    Oct 29 '14 at 5:38











  • It isn't clear what is happening between list and newlist. Will you please clarify.

    – Colin Schoen
    Oct 29 '14 at 5:43











  • I have borrowed from another answer:

    – ProvoWallis
    Oct 29 '14 at 5:43
















3















Apologies if this question has already been asked. I'm a newbie at this but I've looked at a couple of other questions/answers that appear similar to mine but can't find one that does the trick. I've tried Counter but can't seem to figure out how to keep the ID key/value.



I'm trying to sum the values in a Python dictionary using a common key/value. E.g.,



list = [
{'ID':1, 'T2':10, 'T3':20},
{'ID':2, 'T2':5, 'T3':0},
{'ID':1, 'T2':5, 'T3':10},
{'ID':2, 'T2':10, 'T3':30},
{'ID':3, 'T2':5, 'T3':0}
]


but I need this:



newlist = [
{'ID':1, 'T2':15, 'T3':30},
{'ID':2, 'T2':15, 'T3':30},
{'ID':3, 'T2':5, 'T3':0}
]


Any help appreciated.



UPDATE:



I have borrowed from another answer and tried this:



superdict = {}
for d in rows:
for k, v in d.items():
if superdict.get(k) is None:
superdict[k] =
if superdict.get(k) is not None:
superdict[k].append(v)


But instead of keeping getting a new list of combined/added values, I get something like this:



'ID': ['3903', '3997', '3997', '3997', '3947', '4097', '3445', 
'3997', '4107', '3997', '3445', '3997', '3997', '3997',
'3997', '3445', '3997', etc. etc.


UPDATE 2:



Sorry for the confusion in regard to my examples. What I'm looking for is a way to keep the ID value static but add the other values in the dictionary. So all of the values in corresponding to IDs with a value of 1 would be added together.



E.g. this:



list = [{'ID':1, 'T2':10, 'T3':20}, {'ID':1, 'T2':5, 'T3':10}]


Becomes this:



newlist = [{'ID':1, 'T2':15, 'T3':30}]


Hope this helps.










share|improve this question

























  • What is going on in your logic?

    – squiguy
    Oct 29 '14 at 5:38











  • It isn't clear what is happening between list and newlist. Will you please clarify.

    – Colin Schoen
    Oct 29 '14 at 5:43











  • I have borrowed from another answer:

    – ProvoWallis
    Oct 29 '14 at 5:43














3












3








3


1






Apologies if this question has already been asked. I'm a newbie at this but I've looked at a couple of other questions/answers that appear similar to mine but can't find one that does the trick. I've tried Counter but can't seem to figure out how to keep the ID key/value.



I'm trying to sum the values in a Python dictionary using a common key/value. E.g.,



list = [
{'ID':1, 'T2':10, 'T3':20},
{'ID':2, 'T2':5, 'T3':0},
{'ID':1, 'T2':5, 'T3':10},
{'ID':2, 'T2':10, 'T3':30},
{'ID':3, 'T2':5, 'T3':0}
]


but I need this:



newlist = [
{'ID':1, 'T2':15, 'T3':30},
{'ID':2, 'T2':15, 'T3':30},
{'ID':3, 'T2':5, 'T3':0}
]


Any help appreciated.



UPDATE:



I have borrowed from another answer and tried this:



superdict = {}
for d in rows:
for k, v in d.items():
if superdict.get(k) is None:
superdict[k] =
if superdict.get(k) is not None:
superdict[k].append(v)


But instead of keeping getting a new list of combined/added values, I get something like this:



'ID': ['3903', '3997', '3997', '3997', '3947', '4097', '3445', 
'3997', '4107', '3997', '3445', '3997', '3997', '3997',
'3997', '3445', '3997', etc. etc.


UPDATE 2:



Sorry for the confusion in regard to my examples. What I'm looking for is a way to keep the ID value static but add the other values in the dictionary. So all of the values in corresponding to IDs with a value of 1 would be added together.



E.g. this:



list = [{'ID':1, 'T2':10, 'T3':20}, {'ID':1, 'T2':5, 'T3':10}]


Becomes this:



newlist = [{'ID':1, 'T2':15, 'T3':30}]


Hope this helps.










share|improve this question
















Apologies if this question has already been asked. I'm a newbie at this but I've looked at a couple of other questions/answers that appear similar to mine but can't find one that does the trick. I've tried Counter but can't seem to figure out how to keep the ID key/value.



I'm trying to sum the values in a Python dictionary using a common key/value. E.g.,



list = [
{'ID':1, 'T2':10, 'T3':20},
{'ID':2, 'T2':5, 'T3':0},
{'ID':1, 'T2':5, 'T3':10},
{'ID':2, 'T2':10, 'T3':30},
{'ID':3, 'T2':5, 'T3':0}
]


but I need this:



newlist = [
{'ID':1, 'T2':15, 'T3':30},
{'ID':2, 'T2':15, 'T3':30},
{'ID':3, 'T2':5, 'T3':0}
]


Any help appreciated.



UPDATE:



I have borrowed from another answer and tried this:



superdict = {}
for d in rows:
for k, v in d.items():
if superdict.get(k) is None:
superdict[k] =
if superdict.get(k) is not None:
superdict[k].append(v)


But instead of keeping getting a new list of combined/added values, I get something like this:



'ID': ['3903', '3997', '3997', '3997', '3947', '4097', '3445', 
'3997', '4107', '3997', '3445', '3997', '3997', '3997',
'3997', '3445', '3997', etc. etc.


UPDATE 2:



Sorry for the confusion in regard to my examples. What I'm looking for is a way to keep the ID value static but add the other values in the dictionary. So all of the values in corresponding to IDs with a value of 1 would be added together.



E.g. this:



list = [{'ID':1, 'T2':10, 'T3':20}, {'ID':1, 'T2':5, 'T3':10}]


Becomes this:



newlist = [{'ID':1, 'T2':15, 'T3':30}]


Hope this helps.







python dictionary sum






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 4 '18 at 15:44









Charles Merriam

11.3k55362




11.3k55362










asked Oct 29 '14 at 3:12









ProvoWallisProvoWallis

1613




1613













  • What is going on in your logic?

    – squiguy
    Oct 29 '14 at 5:38











  • It isn't clear what is happening between list and newlist. Will you please clarify.

    – Colin Schoen
    Oct 29 '14 at 5:43











  • I have borrowed from another answer:

    – ProvoWallis
    Oct 29 '14 at 5:43



















  • What is going on in your logic?

    – squiguy
    Oct 29 '14 at 5:38











  • It isn't clear what is happening between list and newlist. Will you please clarify.

    – Colin Schoen
    Oct 29 '14 at 5:43











  • I have borrowed from another answer:

    – ProvoWallis
    Oct 29 '14 at 5:43

















What is going on in your logic?

– squiguy
Oct 29 '14 at 5:38





What is going on in your logic?

– squiguy
Oct 29 '14 at 5:38













It isn't clear what is happening between list and newlist. Will you please clarify.

– Colin Schoen
Oct 29 '14 at 5:43





It isn't clear what is happening between list and newlist. Will you please clarify.

– Colin Schoen
Oct 29 '14 at 5:43













I have borrowed from another answer:

– ProvoWallis
Oct 29 '14 at 5:43





I have borrowed from another answer:

– ProvoWallis
Oct 29 '14 at 5:43












4 Answers
4






active

oldest

votes


















3














It's hard to understand what your desired result is supposed to be with your given inputs. Give a concrete example of the expected/correct output.



I suspect this is what you are going for?



my_list = [{'ID':1, 'T2':10, 'T3':20},
{'ID':2, 'T2':5, 'T3':0},
{'ID':1, 'T2':5, 'T3':10},
{'ID':2, 'T2':10, 'T3':30},
{'ID':3, 'T2':5, 'T3':0}]

new_dictionary = {}
for dictionary in my_list:
for key, value in dictionary.items():
if new_dictionary.has_key(key):
new_dictionary[key] = value + new_dictionary[key]
else:
new_dictionary[key] = value

#Output:
>>>new_dictionary
{'T2': 35, 'ID': 9, 'T3': 60}





share|improve this answer































    1














    If I understood your needs correctly, here is a solution:



    def sum_by_common_key(input_list, index_key='ID'):
    output_dict = {}
    for d in input_list:
    index = d[index_key]
    if index not in output_dict:
    output_dict[index] = {}
    for k, v in d.items():
    if k not in output_dict[index]:
    output_dict[index][k] = v
    elif k != index_key:
    output_dict[index][k] += v
    return output_dict.values()

    l = [{'ID':1, 'T2':10, 'T3':20}, {'ID':2, 'T2':5, 'T3':0}, {'ID':1, 'T2':5, 'T3':10}, {'ID':2, 'T2':10, 'T3':30}, {'ID':3, 'T2':5, 'T3':0}]
    print sum_by_common_key(l)

    >>> [{'T2': 15, 'ID': 1, 'T3': 30}, {'T2': 15, 'ID': 2, 'T3': 30}, {'T2': 5, 'ID': 3, 'T3': 0}]





    share|improve this answer































      1














      I hope this will help you:



      >>> from collections import Counter
      >>> old_list=[{'ID':1, 'T2':10, 'T3':20}, {'ID':2, 'T2':5, 'T3':0}, {'ID':1, 'T2':5, 'T3':10}, {'ID':2, 'T2':10, 'T3':30}, {'ID':3, 'T2':5, 'T':0}]
      >>> new_list=
      >>> for i,a in enumerate(old_list):
      ... z=0
      ... for b in old_list[i+1:]:
      ... if a['ID']==b['ID']:
      ... new_list.append(dict(Counter(a)+Counter(b)))
      ... new_list[-1]['ID']=a['ID']
      ... temp=old_list.pop(old_list.index(b))
      ... z=1
      ... if not z:
      ... new_list.append(a)
      ...
      >>> new_list
      [{'T2': 15, 'ID': 1, 'T3': 30}, {'T2': 15, 'ID': 2, 'T3': 30}, {'T2': 5, 'ID': 3, 'T3': 0}]





      share|improve this answer

































        0














        If interpret this properly, the goal is to group by the value for key 'ID' then to sum the values of the corresponding keys within the group. If the keys are consistent i.e. all keys from the first dict are guaranteed to show in the later dicts, here is a a solution:



        my_list = [{'ID':1, 'T2':10, 'T3':20},
        {'ID':2, 'T2':5, 'T3':0},
        {'ID':1, 'T2':5, 'T3':10},
        {'ID':2, 'T2':10, 'T3':30},
        {'ID':3, 'T2':5, 'T3':0}]


        # prep keys
        id_keys = set([ z['ID'] for z in my_list ])
        sum_keys = [ z for z in my_list[0].keys() if z != 'ID' ]
        print('ids to group by', id_keys)
        print('keys to sum over', sum_keys)

        # restructure the data
        big = [ [z.pop('ID'), z] for z in my_list ]
        print('[[group_key, {remaining dict],] :n', big)

        # initiate results accumulator
        rez = {idd: dict.fromkeys(sum_keys, 0) for idd in id_keys }
        print('empty accumulator', rez)

        # two loops in list comprehension
        [ rez[g[0]].update({k: rez[g[0]][k] + g[1][k]}) for k in sum_keys for g in big ]
        print('result', rez)


        Output:



        ids to group by {1, 2, 3}
        keys to sum over ['T2', 'T3']
        [[group_key, {remaining dict],] :
        [[1, {'T2': 10, 'T3': 20}], [2, {'T2': 5, 'T3': 0}], [1, {'T2': 5, 'T3': 10}], [2, {'T2': 10, 'T3': 30}], [3, {'T2': 5, 'T3': 0}]]
        empty accumulator {1: {'T2': 0, 'T3': 0}, 2: {'T2': 0, 'T3': 0}, 3: {'T2': 0, 'T3': 0}}
        result {1: {'T2': 15, 'T3': 30}, 2: {'T2': 15, 'T3': 30}, 3: {'T2': 5, 'T3': 0}}
        [Finished in 0.2s]




        If the goal is to sum over keys, and the keys are not guaranteed to match from one dict to the next, use the following code:



        my_list = [{'T1':1, 'T2':10, 'T3':20},
        {'T1':2, 'T2':5 , 'T3':0},
        { 'T2':5 , },
        { 'T2':10, 'T3':30, 'T4': 7},
        {'T1':3, 'T2':5 , 'T3':0 , 'T4': 3}]

        rez = {}
        for dic in my_list:
        for key in dic.keys():
        rez[key]=rez.setdefault(key, 0) + dic[key]

        print(rez)


        Output:



        {'T1': 6, 'T2': 35, 'T3': 50, 'T4': 10}





        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%2f26622179%2fsum-values-in-a-python-list-of-dictionaries-using-a-common-key-value%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          4 Answers
          4






          active

          oldest

          votes








          4 Answers
          4






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          3














          It's hard to understand what your desired result is supposed to be with your given inputs. Give a concrete example of the expected/correct output.



          I suspect this is what you are going for?



          my_list = [{'ID':1, 'T2':10, 'T3':20},
          {'ID':2, 'T2':5, 'T3':0},
          {'ID':1, 'T2':5, 'T3':10},
          {'ID':2, 'T2':10, 'T3':30},
          {'ID':3, 'T2':5, 'T3':0}]

          new_dictionary = {}
          for dictionary in my_list:
          for key, value in dictionary.items():
          if new_dictionary.has_key(key):
          new_dictionary[key] = value + new_dictionary[key]
          else:
          new_dictionary[key] = value

          #Output:
          >>>new_dictionary
          {'T2': 35, 'ID': 9, 'T3': 60}





          share|improve this answer




























            3














            It's hard to understand what your desired result is supposed to be with your given inputs. Give a concrete example of the expected/correct output.



            I suspect this is what you are going for?



            my_list = [{'ID':1, 'T2':10, 'T3':20},
            {'ID':2, 'T2':5, 'T3':0},
            {'ID':1, 'T2':5, 'T3':10},
            {'ID':2, 'T2':10, 'T3':30},
            {'ID':3, 'T2':5, 'T3':0}]

            new_dictionary = {}
            for dictionary in my_list:
            for key, value in dictionary.items():
            if new_dictionary.has_key(key):
            new_dictionary[key] = value + new_dictionary[key]
            else:
            new_dictionary[key] = value

            #Output:
            >>>new_dictionary
            {'T2': 35, 'ID': 9, 'T3': 60}





            share|improve this answer


























              3












              3








              3







              It's hard to understand what your desired result is supposed to be with your given inputs. Give a concrete example of the expected/correct output.



              I suspect this is what you are going for?



              my_list = [{'ID':1, 'T2':10, 'T3':20},
              {'ID':2, 'T2':5, 'T3':0},
              {'ID':1, 'T2':5, 'T3':10},
              {'ID':2, 'T2':10, 'T3':30},
              {'ID':3, 'T2':5, 'T3':0}]

              new_dictionary = {}
              for dictionary in my_list:
              for key, value in dictionary.items():
              if new_dictionary.has_key(key):
              new_dictionary[key] = value + new_dictionary[key]
              else:
              new_dictionary[key] = value

              #Output:
              >>>new_dictionary
              {'T2': 35, 'ID': 9, 'T3': 60}





              share|improve this answer













              It's hard to understand what your desired result is supposed to be with your given inputs. Give a concrete example of the expected/correct output.



              I suspect this is what you are going for?



              my_list = [{'ID':1, 'T2':10, 'T3':20},
              {'ID':2, 'T2':5, 'T3':0},
              {'ID':1, 'T2':5, 'T3':10},
              {'ID':2, 'T2':10, 'T3':30},
              {'ID':3, 'T2':5, 'T3':0}]

              new_dictionary = {}
              for dictionary in my_list:
              for key, value in dictionary.items():
              if new_dictionary.has_key(key):
              new_dictionary[key] = value + new_dictionary[key]
              else:
              new_dictionary[key] = value

              #Output:
              >>>new_dictionary
              {'T2': 35, 'ID': 9, 'T3': 60}






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Oct 29 '14 at 6:55









              theNamesCrosstheNamesCross

              4131517




              4131517

























                  1














                  If I understood your needs correctly, here is a solution:



                  def sum_by_common_key(input_list, index_key='ID'):
                  output_dict = {}
                  for d in input_list:
                  index = d[index_key]
                  if index not in output_dict:
                  output_dict[index] = {}
                  for k, v in d.items():
                  if k not in output_dict[index]:
                  output_dict[index][k] = v
                  elif k != index_key:
                  output_dict[index][k] += v
                  return output_dict.values()

                  l = [{'ID':1, 'T2':10, 'T3':20}, {'ID':2, 'T2':5, 'T3':0}, {'ID':1, 'T2':5, 'T3':10}, {'ID':2, 'T2':10, 'T3':30}, {'ID':3, 'T2':5, 'T3':0}]
                  print sum_by_common_key(l)

                  >>> [{'T2': 15, 'ID': 1, 'T3': 30}, {'T2': 15, 'ID': 2, 'T3': 30}, {'T2': 5, 'ID': 3, 'T3': 0}]





                  share|improve this answer




























                    1














                    If I understood your needs correctly, here is a solution:



                    def sum_by_common_key(input_list, index_key='ID'):
                    output_dict = {}
                    for d in input_list:
                    index = d[index_key]
                    if index not in output_dict:
                    output_dict[index] = {}
                    for k, v in d.items():
                    if k not in output_dict[index]:
                    output_dict[index][k] = v
                    elif k != index_key:
                    output_dict[index][k] += v
                    return output_dict.values()

                    l = [{'ID':1, 'T2':10, 'T3':20}, {'ID':2, 'T2':5, 'T3':0}, {'ID':1, 'T2':5, 'T3':10}, {'ID':2, 'T2':10, 'T3':30}, {'ID':3, 'T2':5, 'T3':0}]
                    print sum_by_common_key(l)

                    >>> [{'T2': 15, 'ID': 1, 'T3': 30}, {'T2': 15, 'ID': 2, 'T3': 30}, {'T2': 5, 'ID': 3, 'T3': 0}]





                    share|improve this answer


























                      1












                      1








                      1







                      If I understood your needs correctly, here is a solution:



                      def sum_by_common_key(input_list, index_key='ID'):
                      output_dict = {}
                      for d in input_list:
                      index = d[index_key]
                      if index not in output_dict:
                      output_dict[index] = {}
                      for k, v in d.items():
                      if k not in output_dict[index]:
                      output_dict[index][k] = v
                      elif k != index_key:
                      output_dict[index][k] += v
                      return output_dict.values()

                      l = [{'ID':1, 'T2':10, 'T3':20}, {'ID':2, 'T2':5, 'T3':0}, {'ID':1, 'T2':5, 'T3':10}, {'ID':2, 'T2':10, 'T3':30}, {'ID':3, 'T2':5, 'T3':0}]
                      print sum_by_common_key(l)

                      >>> [{'T2': 15, 'ID': 1, 'T3': 30}, {'T2': 15, 'ID': 2, 'T3': 30}, {'T2': 5, 'ID': 3, 'T3': 0}]





                      share|improve this answer













                      If I understood your needs correctly, here is a solution:



                      def sum_by_common_key(input_list, index_key='ID'):
                      output_dict = {}
                      for d in input_list:
                      index = d[index_key]
                      if index not in output_dict:
                      output_dict[index] = {}
                      for k, v in d.items():
                      if k not in output_dict[index]:
                      output_dict[index][k] = v
                      elif k != index_key:
                      output_dict[index][k] += v
                      return output_dict.values()

                      l = [{'ID':1, 'T2':10, 'T3':20}, {'ID':2, 'T2':5, 'T3':0}, {'ID':1, 'T2':5, 'T3':10}, {'ID':2, 'T2':10, 'T3':30}, {'ID':3, 'T2':5, 'T3':0}]
                      print sum_by_common_key(l)

                      >>> [{'T2': 15, 'ID': 1, 'T3': 30}, {'T2': 15, 'ID': 2, 'T3': 30}, {'T2': 5, 'ID': 3, 'T3': 0}]






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Oct 29 '14 at 6:48









                      ZZYZZY

                      2,5731118




                      2,5731118























                          1














                          I hope this will help you:



                          >>> from collections import Counter
                          >>> old_list=[{'ID':1, 'T2':10, 'T3':20}, {'ID':2, 'T2':5, 'T3':0}, {'ID':1, 'T2':5, 'T3':10}, {'ID':2, 'T2':10, 'T3':30}, {'ID':3, 'T2':5, 'T':0}]
                          >>> new_list=
                          >>> for i,a in enumerate(old_list):
                          ... z=0
                          ... for b in old_list[i+1:]:
                          ... if a['ID']==b['ID']:
                          ... new_list.append(dict(Counter(a)+Counter(b)))
                          ... new_list[-1]['ID']=a['ID']
                          ... temp=old_list.pop(old_list.index(b))
                          ... z=1
                          ... if not z:
                          ... new_list.append(a)
                          ...
                          >>> new_list
                          [{'T2': 15, 'ID': 1, 'T3': 30}, {'T2': 15, 'ID': 2, 'T3': 30}, {'T2': 5, 'ID': 3, 'T3': 0}]





                          share|improve this answer






























                            1














                            I hope this will help you:



                            >>> from collections import Counter
                            >>> old_list=[{'ID':1, 'T2':10, 'T3':20}, {'ID':2, 'T2':5, 'T3':0}, {'ID':1, 'T2':5, 'T3':10}, {'ID':2, 'T2':10, 'T3':30}, {'ID':3, 'T2':5, 'T':0}]
                            >>> new_list=
                            >>> for i,a in enumerate(old_list):
                            ... z=0
                            ... for b in old_list[i+1:]:
                            ... if a['ID']==b['ID']:
                            ... new_list.append(dict(Counter(a)+Counter(b)))
                            ... new_list[-1]['ID']=a['ID']
                            ... temp=old_list.pop(old_list.index(b))
                            ... z=1
                            ... if not z:
                            ... new_list.append(a)
                            ...
                            >>> new_list
                            [{'T2': 15, 'ID': 1, 'T3': 30}, {'T2': 15, 'ID': 2, 'T3': 30}, {'T2': 5, 'ID': 3, 'T3': 0}]





                            share|improve this answer




























                              1












                              1








                              1







                              I hope this will help you:



                              >>> from collections import Counter
                              >>> old_list=[{'ID':1, 'T2':10, 'T3':20}, {'ID':2, 'T2':5, 'T3':0}, {'ID':1, 'T2':5, 'T3':10}, {'ID':2, 'T2':10, 'T3':30}, {'ID':3, 'T2':5, 'T':0}]
                              >>> new_list=
                              >>> for i,a in enumerate(old_list):
                              ... z=0
                              ... for b in old_list[i+1:]:
                              ... if a['ID']==b['ID']:
                              ... new_list.append(dict(Counter(a)+Counter(b)))
                              ... new_list[-1]['ID']=a['ID']
                              ... temp=old_list.pop(old_list.index(b))
                              ... z=1
                              ... if not z:
                              ... new_list.append(a)
                              ...
                              >>> new_list
                              [{'T2': 15, 'ID': 1, 'T3': 30}, {'T2': 15, 'ID': 2, 'T3': 30}, {'T2': 5, 'ID': 3, 'T3': 0}]





                              share|improve this answer















                              I hope this will help you:



                              >>> from collections import Counter
                              >>> old_list=[{'ID':1, 'T2':10, 'T3':20}, {'ID':2, 'T2':5, 'T3':0}, {'ID':1, 'T2':5, 'T3':10}, {'ID':2, 'T2':10, 'T3':30}, {'ID':3, 'T2':5, 'T':0}]
                              >>> new_list=
                              >>> for i,a in enumerate(old_list):
                              ... z=0
                              ... for b in old_list[i+1:]:
                              ... if a['ID']==b['ID']:
                              ... new_list.append(dict(Counter(a)+Counter(b)))
                              ... new_list[-1]['ID']=a['ID']
                              ... temp=old_list.pop(old_list.index(b))
                              ... z=1
                              ... if not z:
                              ... new_list.append(a)
                              ...
                              >>> new_list
                              [{'T2': 15, 'ID': 1, 'T3': 30}, {'T2': 15, 'ID': 2, 'T3': 30}, {'T2': 5, 'ID': 3, 'T3': 0}]






                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              edited Oct 29 '14 at 7:12

























                              answered Oct 29 '14 at 7:03









                              Irshad BhatIrshad Bhat

                              5,82311226




                              5,82311226























                                  0














                                  If interpret this properly, the goal is to group by the value for key 'ID' then to sum the values of the corresponding keys within the group. If the keys are consistent i.e. all keys from the first dict are guaranteed to show in the later dicts, here is a a solution:



                                  my_list = [{'ID':1, 'T2':10, 'T3':20},
                                  {'ID':2, 'T2':5, 'T3':0},
                                  {'ID':1, 'T2':5, 'T3':10},
                                  {'ID':2, 'T2':10, 'T3':30},
                                  {'ID':3, 'T2':5, 'T3':0}]


                                  # prep keys
                                  id_keys = set([ z['ID'] for z in my_list ])
                                  sum_keys = [ z for z in my_list[0].keys() if z != 'ID' ]
                                  print('ids to group by', id_keys)
                                  print('keys to sum over', sum_keys)

                                  # restructure the data
                                  big = [ [z.pop('ID'), z] for z in my_list ]
                                  print('[[group_key, {remaining dict],] :n', big)

                                  # initiate results accumulator
                                  rez = {idd: dict.fromkeys(sum_keys, 0) for idd in id_keys }
                                  print('empty accumulator', rez)

                                  # two loops in list comprehension
                                  [ rez[g[0]].update({k: rez[g[0]][k] + g[1][k]}) for k in sum_keys for g in big ]
                                  print('result', rez)


                                  Output:



                                  ids to group by {1, 2, 3}
                                  keys to sum over ['T2', 'T3']
                                  [[group_key, {remaining dict],] :
                                  [[1, {'T2': 10, 'T3': 20}], [2, {'T2': 5, 'T3': 0}], [1, {'T2': 5, 'T3': 10}], [2, {'T2': 10, 'T3': 30}], [3, {'T2': 5, 'T3': 0}]]
                                  empty accumulator {1: {'T2': 0, 'T3': 0}, 2: {'T2': 0, 'T3': 0}, 3: {'T2': 0, 'T3': 0}}
                                  result {1: {'T2': 15, 'T3': 30}, 2: {'T2': 15, 'T3': 30}, 3: {'T2': 5, 'T3': 0}}
                                  [Finished in 0.2s]




                                  If the goal is to sum over keys, and the keys are not guaranteed to match from one dict to the next, use the following code:



                                  my_list = [{'T1':1, 'T2':10, 'T3':20},
                                  {'T1':2, 'T2':5 , 'T3':0},
                                  { 'T2':5 , },
                                  { 'T2':10, 'T3':30, 'T4': 7},
                                  {'T1':3, 'T2':5 , 'T3':0 , 'T4': 3}]

                                  rez = {}
                                  for dic in my_list:
                                  for key in dic.keys():
                                  rez[key]=rez.setdefault(key, 0) + dic[key]

                                  print(rez)


                                  Output:



                                  {'T1': 6, 'T2': 35, 'T3': 50, 'T4': 10}





                                  share|improve this answer




























                                    0














                                    If interpret this properly, the goal is to group by the value for key 'ID' then to sum the values of the corresponding keys within the group. If the keys are consistent i.e. all keys from the first dict are guaranteed to show in the later dicts, here is a a solution:



                                    my_list = [{'ID':1, 'T2':10, 'T3':20},
                                    {'ID':2, 'T2':5, 'T3':0},
                                    {'ID':1, 'T2':5, 'T3':10},
                                    {'ID':2, 'T2':10, 'T3':30},
                                    {'ID':3, 'T2':5, 'T3':0}]


                                    # prep keys
                                    id_keys = set([ z['ID'] for z in my_list ])
                                    sum_keys = [ z for z in my_list[0].keys() if z != 'ID' ]
                                    print('ids to group by', id_keys)
                                    print('keys to sum over', sum_keys)

                                    # restructure the data
                                    big = [ [z.pop('ID'), z] for z in my_list ]
                                    print('[[group_key, {remaining dict],] :n', big)

                                    # initiate results accumulator
                                    rez = {idd: dict.fromkeys(sum_keys, 0) for idd in id_keys }
                                    print('empty accumulator', rez)

                                    # two loops in list comprehension
                                    [ rez[g[0]].update({k: rez[g[0]][k] + g[1][k]}) for k in sum_keys for g in big ]
                                    print('result', rez)


                                    Output:



                                    ids to group by {1, 2, 3}
                                    keys to sum over ['T2', 'T3']
                                    [[group_key, {remaining dict],] :
                                    [[1, {'T2': 10, 'T3': 20}], [2, {'T2': 5, 'T3': 0}], [1, {'T2': 5, 'T3': 10}], [2, {'T2': 10, 'T3': 30}], [3, {'T2': 5, 'T3': 0}]]
                                    empty accumulator {1: {'T2': 0, 'T3': 0}, 2: {'T2': 0, 'T3': 0}, 3: {'T2': 0, 'T3': 0}}
                                    result {1: {'T2': 15, 'T3': 30}, 2: {'T2': 15, 'T3': 30}, 3: {'T2': 5, 'T3': 0}}
                                    [Finished in 0.2s]




                                    If the goal is to sum over keys, and the keys are not guaranteed to match from one dict to the next, use the following code:



                                    my_list = [{'T1':1, 'T2':10, 'T3':20},
                                    {'T1':2, 'T2':5 , 'T3':0},
                                    { 'T2':5 , },
                                    { 'T2':10, 'T3':30, 'T4': 7},
                                    {'T1':3, 'T2':5 , 'T3':0 , 'T4': 3}]

                                    rez = {}
                                    for dic in my_list:
                                    for key in dic.keys():
                                    rez[key]=rez.setdefault(key, 0) + dic[key]

                                    print(rez)


                                    Output:



                                    {'T1': 6, 'T2': 35, 'T3': 50, 'T4': 10}





                                    share|improve this answer


























                                      0












                                      0








                                      0







                                      If interpret this properly, the goal is to group by the value for key 'ID' then to sum the values of the corresponding keys within the group. If the keys are consistent i.e. all keys from the first dict are guaranteed to show in the later dicts, here is a a solution:



                                      my_list = [{'ID':1, 'T2':10, 'T3':20},
                                      {'ID':2, 'T2':5, 'T3':0},
                                      {'ID':1, 'T2':5, 'T3':10},
                                      {'ID':2, 'T2':10, 'T3':30},
                                      {'ID':3, 'T2':5, 'T3':0}]


                                      # prep keys
                                      id_keys = set([ z['ID'] for z in my_list ])
                                      sum_keys = [ z for z in my_list[0].keys() if z != 'ID' ]
                                      print('ids to group by', id_keys)
                                      print('keys to sum over', sum_keys)

                                      # restructure the data
                                      big = [ [z.pop('ID'), z] for z in my_list ]
                                      print('[[group_key, {remaining dict],] :n', big)

                                      # initiate results accumulator
                                      rez = {idd: dict.fromkeys(sum_keys, 0) for idd in id_keys }
                                      print('empty accumulator', rez)

                                      # two loops in list comprehension
                                      [ rez[g[0]].update({k: rez[g[0]][k] + g[1][k]}) for k in sum_keys for g in big ]
                                      print('result', rez)


                                      Output:



                                      ids to group by {1, 2, 3}
                                      keys to sum over ['T2', 'T3']
                                      [[group_key, {remaining dict],] :
                                      [[1, {'T2': 10, 'T3': 20}], [2, {'T2': 5, 'T3': 0}], [1, {'T2': 5, 'T3': 10}], [2, {'T2': 10, 'T3': 30}], [3, {'T2': 5, 'T3': 0}]]
                                      empty accumulator {1: {'T2': 0, 'T3': 0}, 2: {'T2': 0, 'T3': 0}, 3: {'T2': 0, 'T3': 0}}
                                      result {1: {'T2': 15, 'T3': 30}, 2: {'T2': 15, 'T3': 30}, 3: {'T2': 5, 'T3': 0}}
                                      [Finished in 0.2s]




                                      If the goal is to sum over keys, and the keys are not guaranteed to match from one dict to the next, use the following code:



                                      my_list = [{'T1':1, 'T2':10, 'T3':20},
                                      {'T1':2, 'T2':5 , 'T3':0},
                                      { 'T2':5 , },
                                      { 'T2':10, 'T3':30, 'T4': 7},
                                      {'T1':3, 'T2':5 , 'T3':0 , 'T4': 3}]

                                      rez = {}
                                      for dic in my_list:
                                      for key in dic.keys():
                                      rez[key]=rez.setdefault(key, 0) + dic[key]

                                      print(rez)


                                      Output:



                                      {'T1': 6, 'T2': 35, 'T3': 50, 'T4': 10}





                                      share|improve this answer













                                      If interpret this properly, the goal is to group by the value for key 'ID' then to sum the values of the corresponding keys within the group. If the keys are consistent i.e. all keys from the first dict are guaranteed to show in the later dicts, here is a a solution:



                                      my_list = [{'ID':1, 'T2':10, 'T3':20},
                                      {'ID':2, 'T2':5, 'T3':0},
                                      {'ID':1, 'T2':5, 'T3':10},
                                      {'ID':2, 'T2':10, 'T3':30},
                                      {'ID':3, 'T2':5, 'T3':0}]


                                      # prep keys
                                      id_keys = set([ z['ID'] for z in my_list ])
                                      sum_keys = [ z for z in my_list[0].keys() if z != 'ID' ]
                                      print('ids to group by', id_keys)
                                      print('keys to sum over', sum_keys)

                                      # restructure the data
                                      big = [ [z.pop('ID'), z] for z in my_list ]
                                      print('[[group_key, {remaining dict],] :n', big)

                                      # initiate results accumulator
                                      rez = {idd: dict.fromkeys(sum_keys, 0) for idd in id_keys }
                                      print('empty accumulator', rez)

                                      # two loops in list comprehension
                                      [ rez[g[0]].update({k: rez[g[0]][k] + g[1][k]}) for k in sum_keys for g in big ]
                                      print('result', rez)


                                      Output:



                                      ids to group by {1, 2, 3}
                                      keys to sum over ['T2', 'T3']
                                      [[group_key, {remaining dict],] :
                                      [[1, {'T2': 10, 'T3': 20}], [2, {'T2': 5, 'T3': 0}], [1, {'T2': 5, 'T3': 10}], [2, {'T2': 10, 'T3': 30}], [3, {'T2': 5, 'T3': 0}]]
                                      empty accumulator {1: {'T2': 0, 'T3': 0}, 2: {'T2': 0, 'T3': 0}, 3: {'T2': 0, 'T3': 0}}
                                      result {1: {'T2': 15, 'T3': 30}, 2: {'T2': 15, 'T3': 30}, 3: {'T2': 5, 'T3': 0}}
                                      [Finished in 0.2s]




                                      If the goal is to sum over keys, and the keys are not guaranteed to match from one dict to the next, use the following code:



                                      my_list = [{'T1':1, 'T2':10, 'T3':20},
                                      {'T1':2, 'T2':5 , 'T3':0},
                                      { 'T2':5 , },
                                      { 'T2':10, 'T3':30, 'T4': 7},
                                      {'T1':3, 'T2':5 , 'T3':0 , 'T4': 3}]

                                      rez = {}
                                      for dic in my_list:
                                      for key in dic.keys():
                                      rez[key]=rez.setdefault(key, 0) + dic[key]

                                      print(rez)


                                      Output:



                                      {'T1': 6, 'T2': 35, 'T3': 50, 'T4': 10}






                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Feb 15 '18 at 18:16









                                      Iliyan BobevIliyan Bobev

                                      1,99511322




                                      1,99511322






























                                          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%2f26622179%2fsum-values-in-a-python-list-of-dictionaries-using-a-common-key-value%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

                                          MongoDB - Not Authorized To Execute Command

                                          in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

                                          How to fix TextFormField cause rebuild widget in Flutter