how to sum values from all objects within one dictionary in python











up vote
-1
down vote

favorite












I have the following dict:



data = {
'test1': {
'x1': {
'z1': 22,
'z2': 11,
'z3': 21,
},
'x2': {
'z1': 15,
'z2': 34,
'z3': 54,
}
},
'test2': {
'x1': {
'z1': 22,
'z2': 11,
'z3': 21,
},
'x2': {
'z1': 15,
'z2': 34,
'z3': 54,
}
}
}


And what I would like to do is to sum all z2 objects within 'test1' and ,in this case, get 45



I know that I can do this by:



data['test1']['x1']['z2'] + data['test1']['x2']['z2']


but would like to know if there is a chance to take it in one code, for example



data['test1'][ * ]['z2'] where the star would represent all objects ( like in excel - this example doesn't work of course



Wondering if there is a better solution for this problem



Will be thankful for your support










share|improve this question


























    up vote
    -1
    down vote

    favorite












    I have the following dict:



    data = {
    'test1': {
    'x1': {
    'z1': 22,
    'z2': 11,
    'z3': 21,
    },
    'x2': {
    'z1': 15,
    'z2': 34,
    'z3': 54,
    }
    },
    'test2': {
    'x1': {
    'z1': 22,
    'z2': 11,
    'z3': 21,
    },
    'x2': {
    'z1': 15,
    'z2': 34,
    'z3': 54,
    }
    }
    }


    And what I would like to do is to sum all z2 objects within 'test1' and ,in this case, get 45



    I know that I can do this by:



    data['test1']['x1']['z2'] + data['test1']['x2']['z2']


    but would like to know if there is a chance to take it in one code, for example



    data['test1'][ * ]['z2'] where the star would represent all objects ( like in excel - this example doesn't work of course



    Wondering if there is a better solution for this problem



    Will be thankful for your support










    share|improve this question
























      up vote
      -1
      down vote

      favorite









      up vote
      -1
      down vote

      favorite











      I have the following dict:



      data = {
      'test1': {
      'x1': {
      'z1': 22,
      'z2': 11,
      'z3': 21,
      },
      'x2': {
      'z1': 15,
      'z2': 34,
      'z3': 54,
      }
      },
      'test2': {
      'x1': {
      'z1': 22,
      'z2': 11,
      'z3': 21,
      },
      'x2': {
      'z1': 15,
      'z2': 34,
      'z3': 54,
      }
      }
      }


      And what I would like to do is to sum all z2 objects within 'test1' and ,in this case, get 45



      I know that I can do this by:



      data['test1']['x1']['z2'] + data['test1']['x2']['z2']


      but would like to know if there is a chance to take it in one code, for example



      data['test1'][ * ]['z2'] where the star would represent all objects ( like in excel - this example doesn't work of course



      Wondering if there is a better solution for this problem



      Will be thankful for your support










      share|improve this question













      I have the following dict:



      data = {
      'test1': {
      'x1': {
      'z1': 22,
      'z2': 11,
      'z3': 21,
      },
      'x2': {
      'z1': 15,
      'z2': 34,
      'z3': 54,
      }
      },
      'test2': {
      'x1': {
      'z1': 22,
      'z2': 11,
      'z3': 21,
      },
      'x2': {
      'z1': 15,
      'z2': 34,
      'z3': 54,
      }
      }
      }


      And what I would like to do is to sum all z2 objects within 'test1' and ,in this case, get 45



      I know that I can do this by:



      data['test1']['x1']['z2'] + data['test1']['x2']['z2']


      but would like to know if there is a chance to take it in one code, for example



      data['test1'][ * ]['z2'] where the star would represent all objects ( like in excel - this example doesn't work of course



      Wondering if there is a better solution for this problem



      Will be thankful for your support







      python python-3.x






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 10 at 16:03









      akaribi

      192




      192
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted










          You can use sum:



          d = {'test1': {'x1': {'z1': 22, 'z2': 11, 'z3': 21}, 'x2': {'z1': 15, 'z2': 34, 'z3': 54}}, 'test2': {'x1': {'z1': 22, 'z2': 11, 'z3': 21}, 'x2': {'z1': 15, 'z2': 34, 'z3': 54}}}
          result = sum(b['z2'] for a, b in d['test1'].items())


          Output:



          45


          Edit: without an explicit loop, you can use reduce:



          from functools import reduce
          new_result = reduce(lambda x, y:x+y['z2'], d['test1'].values(), 0)


          Output:



          45





          share|improve this answer



















          • 1




            Or just iterate d['test1'].values() ?
            – jpp
            Nov 10 at 16:07










          • This is the option, but is there a way to do this without a loop ?
            – akaribi
            Nov 10 at 16:07






          • 3




            @akaribi how do you imagine repeating an operation over multiple items without a loop would work? What exactly about this answer isn't suitable for you?
            – Jon Clements
            Nov 10 at 16:08












          • @akaribi Please see my recent edit.
            – Ajax1234
            Nov 10 at 16:10










          • thanks man ! perfect
            – akaribi
            Nov 10 at 16:19


















          up vote
          0
          down vote













          I think, Python's pandas library would be a great to easily tackle this kind of problems.



          Please have a look at the below code.



          >>> import pandas as pd
          >>>
          >>> data = {
          ... 'test1': {
          ... 'x1': {
          ... 'z1': 22,
          ... 'z2': 11,
          ... 'z3': 21,
          ... },
          ... 'x2': {
          ... 'z1': 15,
          ... 'z2': 34,
          ... 'z3': 54,
          ... }
          ... },
          ... 'test2': {
          ... 'x1': {
          ... 'z1': 22,
          ... 'z2': 11,
          ... 'z3': 21,
          ... },
          ... 'x2': {
          ... 'z1': 15,
          ... 'z2': 34,
          ... 'z3': 54,
          ... }
          ... }
          ... }
          >>>
          >>> d = data["test1"]
          >>> d
          {'x1': {'z1': 22, 'z2': 11, 'z3': 21}, 'x2': {'z1': 15, 'z2': 34, 'z3': 54}}
          >>>
          >>> df = pd.DataFrame(list(d.values()), index=list(d.keys()))
          >>> df
          z1 z2 z3
          x1 22 11 21
          x2 15 34 54
          >>>
          >>> df.z2.sum() # 1st way
          45
          >>>
          >>> df["z2"].sum() # 2nd way
          45
          >>>


          References »


          • https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.sum.html






          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',
            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%2f53240766%2fhow-to-sum-values-from-all-objects-within-one-dictionary-in-python%23new-answer', 'question_page');
            }
            );

            Post as a guest
































            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            0
            down vote



            accepted










            You can use sum:



            d = {'test1': {'x1': {'z1': 22, 'z2': 11, 'z3': 21}, 'x2': {'z1': 15, 'z2': 34, 'z3': 54}}, 'test2': {'x1': {'z1': 22, 'z2': 11, 'z3': 21}, 'x2': {'z1': 15, 'z2': 34, 'z3': 54}}}
            result = sum(b['z2'] for a, b in d['test1'].items())


            Output:



            45


            Edit: without an explicit loop, you can use reduce:



            from functools import reduce
            new_result = reduce(lambda x, y:x+y['z2'], d['test1'].values(), 0)


            Output:



            45





            share|improve this answer



















            • 1




              Or just iterate d['test1'].values() ?
              – jpp
              Nov 10 at 16:07










            • This is the option, but is there a way to do this without a loop ?
              – akaribi
              Nov 10 at 16:07






            • 3




              @akaribi how do you imagine repeating an operation over multiple items without a loop would work? What exactly about this answer isn't suitable for you?
              – Jon Clements
              Nov 10 at 16:08












            • @akaribi Please see my recent edit.
              – Ajax1234
              Nov 10 at 16:10










            • thanks man ! perfect
              – akaribi
              Nov 10 at 16:19















            up vote
            0
            down vote



            accepted










            You can use sum:



            d = {'test1': {'x1': {'z1': 22, 'z2': 11, 'z3': 21}, 'x2': {'z1': 15, 'z2': 34, 'z3': 54}}, 'test2': {'x1': {'z1': 22, 'z2': 11, 'z3': 21}, 'x2': {'z1': 15, 'z2': 34, 'z3': 54}}}
            result = sum(b['z2'] for a, b in d['test1'].items())


            Output:



            45


            Edit: without an explicit loop, you can use reduce:



            from functools import reduce
            new_result = reduce(lambda x, y:x+y['z2'], d['test1'].values(), 0)


            Output:



            45





            share|improve this answer



















            • 1




              Or just iterate d['test1'].values() ?
              – jpp
              Nov 10 at 16:07










            • This is the option, but is there a way to do this without a loop ?
              – akaribi
              Nov 10 at 16:07






            • 3




              @akaribi how do you imagine repeating an operation over multiple items without a loop would work? What exactly about this answer isn't suitable for you?
              – Jon Clements
              Nov 10 at 16:08












            • @akaribi Please see my recent edit.
              – Ajax1234
              Nov 10 at 16:10










            • thanks man ! perfect
              – akaribi
              Nov 10 at 16:19













            up vote
            0
            down vote



            accepted







            up vote
            0
            down vote



            accepted






            You can use sum:



            d = {'test1': {'x1': {'z1': 22, 'z2': 11, 'z3': 21}, 'x2': {'z1': 15, 'z2': 34, 'z3': 54}}, 'test2': {'x1': {'z1': 22, 'z2': 11, 'z3': 21}, 'x2': {'z1': 15, 'z2': 34, 'z3': 54}}}
            result = sum(b['z2'] for a, b in d['test1'].items())


            Output:



            45


            Edit: without an explicit loop, you can use reduce:



            from functools import reduce
            new_result = reduce(lambda x, y:x+y['z2'], d['test1'].values(), 0)


            Output:



            45





            share|improve this answer














            You can use sum:



            d = {'test1': {'x1': {'z1': 22, 'z2': 11, 'z3': 21}, 'x2': {'z1': 15, 'z2': 34, 'z3': 54}}, 'test2': {'x1': {'z1': 22, 'z2': 11, 'z3': 21}, 'x2': {'z1': 15, 'z2': 34, 'z3': 54}}}
            result = sum(b['z2'] for a, b in d['test1'].items())


            Output:



            45


            Edit: without an explicit loop, you can use reduce:



            from functools import reduce
            new_result = reduce(lambda x, y:x+y['z2'], d['test1'].values(), 0)


            Output:



            45






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 10 at 16:10

























            answered Nov 10 at 16:05









            Ajax1234

            38.3k42351




            38.3k42351








            • 1




              Or just iterate d['test1'].values() ?
              – jpp
              Nov 10 at 16:07










            • This is the option, but is there a way to do this without a loop ?
              – akaribi
              Nov 10 at 16:07






            • 3




              @akaribi how do you imagine repeating an operation over multiple items without a loop would work? What exactly about this answer isn't suitable for you?
              – Jon Clements
              Nov 10 at 16:08












            • @akaribi Please see my recent edit.
              – Ajax1234
              Nov 10 at 16:10










            • thanks man ! perfect
              – akaribi
              Nov 10 at 16:19














            • 1




              Or just iterate d['test1'].values() ?
              – jpp
              Nov 10 at 16:07










            • This is the option, but is there a way to do this without a loop ?
              – akaribi
              Nov 10 at 16:07






            • 3




              @akaribi how do you imagine repeating an operation over multiple items without a loop would work? What exactly about this answer isn't suitable for you?
              – Jon Clements
              Nov 10 at 16:08












            • @akaribi Please see my recent edit.
              – Ajax1234
              Nov 10 at 16:10










            • thanks man ! perfect
              – akaribi
              Nov 10 at 16:19








            1




            1




            Or just iterate d['test1'].values() ?
            – jpp
            Nov 10 at 16:07




            Or just iterate d['test1'].values() ?
            – jpp
            Nov 10 at 16:07












            This is the option, but is there a way to do this without a loop ?
            – akaribi
            Nov 10 at 16:07




            This is the option, but is there a way to do this without a loop ?
            – akaribi
            Nov 10 at 16:07




            3




            3




            @akaribi how do you imagine repeating an operation over multiple items without a loop would work? What exactly about this answer isn't suitable for you?
            – Jon Clements
            Nov 10 at 16:08






            @akaribi how do you imagine repeating an operation over multiple items without a loop would work? What exactly about this answer isn't suitable for you?
            – Jon Clements
            Nov 10 at 16:08














            @akaribi Please see my recent edit.
            – Ajax1234
            Nov 10 at 16:10




            @akaribi Please see my recent edit.
            – Ajax1234
            Nov 10 at 16:10












            thanks man ! perfect
            – akaribi
            Nov 10 at 16:19




            thanks man ! perfect
            – akaribi
            Nov 10 at 16:19












            up vote
            0
            down vote













            I think, Python's pandas library would be a great to easily tackle this kind of problems.



            Please have a look at the below code.



            >>> import pandas as pd
            >>>
            >>> data = {
            ... 'test1': {
            ... 'x1': {
            ... 'z1': 22,
            ... 'z2': 11,
            ... 'z3': 21,
            ... },
            ... 'x2': {
            ... 'z1': 15,
            ... 'z2': 34,
            ... 'z3': 54,
            ... }
            ... },
            ... 'test2': {
            ... 'x1': {
            ... 'z1': 22,
            ... 'z2': 11,
            ... 'z3': 21,
            ... },
            ... 'x2': {
            ... 'z1': 15,
            ... 'z2': 34,
            ... 'z3': 54,
            ... }
            ... }
            ... }
            >>>
            >>> d = data["test1"]
            >>> d
            {'x1': {'z1': 22, 'z2': 11, 'z3': 21}, 'x2': {'z1': 15, 'z2': 34, 'z3': 54}}
            >>>
            >>> df = pd.DataFrame(list(d.values()), index=list(d.keys()))
            >>> df
            z1 z2 z3
            x1 22 11 21
            x2 15 34 54
            >>>
            >>> df.z2.sum() # 1st way
            45
            >>>
            >>> df["z2"].sum() # 2nd way
            45
            >>>


            References »


            • https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.sum.html






            share|improve this answer

























              up vote
              0
              down vote













              I think, Python's pandas library would be a great to easily tackle this kind of problems.



              Please have a look at the below code.



              >>> import pandas as pd
              >>>
              >>> data = {
              ... 'test1': {
              ... 'x1': {
              ... 'z1': 22,
              ... 'z2': 11,
              ... 'z3': 21,
              ... },
              ... 'x2': {
              ... 'z1': 15,
              ... 'z2': 34,
              ... 'z3': 54,
              ... }
              ... },
              ... 'test2': {
              ... 'x1': {
              ... 'z1': 22,
              ... 'z2': 11,
              ... 'z3': 21,
              ... },
              ... 'x2': {
              ... 'z1': 15,
              ... 'z2': 34,
              ... 'z3': 54,
              ... }
              ... }
              ... }
              >>>
              >>> d = data["test1"]
              >>> d
              {'x1': {'z1': 22, 'z2': 11, 'z3': 21}, 'x2': {'z1': 15, 'z2': 34, 'z3': 54}}
              >>>
              >>> df = pd.DataFrame(list(d.values()), index=list(d.keys()))
              >>> df
              z1 z2 z3
              x1 22 11 21
              x2 15 34 54
              >>>
              >>> df.z2.sum() # 1st way
              45
              >>>
              >>> df["z2"].sum() # 2nd way
              45
              >>>


              References »


              • https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.sum.html






              share|improve this answer























                up vote
                0
                down vote










                up vote
                0
                down vote









                I think, Python's pandas library would be a great to easily tackle this kind of problems.



                Please have a look at the below code.



                >>> import pandas as pd
                >>>
                >>> data = {
                ... 'test1': {
                ... 'x1': {
                ... 'z1': 22,
                ... 'z2': 11,
                ... 'z3': 21,
                ... },
                ... 'x2': {
                ... 'z1': 15,
                ... 'z2': 34,
                ... 'z3': 54,
                ... }
                ... },
                ... 'test2': {
                ... 'x1': {
                ... 'z1': 22,
                ... 'z2': 11,
                ... 'z3': 21,
                ... },
                ... 'x2': {
                ... 'z1': 15,
                ... 'z2': 34,
                ... 'z3': 54,
                ... }
                ... }
                ... }
                >>>
                >>> d = data["test1"]
                >>> d
                {'x1': {'z1': 22, 'z2': 11, 'z3': 21}, 'x2': {'z1': 15, 'z2': 34, 'z3': 54}}
                >>>
                >>> df = pd.DataFrame(list(d.values()), index=list(d.keys()))
                >>> df
                z1 z2 z3
                x1 22 11 21
                x2 15 34 54
                >>>
                >>> df.z2.sum() # 1st way
                45
                >>>
                >>> df["z2"].sum() # 2nd way
                45
                >>>


                References »


                • https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.sum.html






                share|improve this answer












                I think, Python's pandas library would be a great to easily tackle this kind of problems.



                Please have a look at the below code.



                >>> import pandas as pd
                >>>
                >>> data = {
                ... 'test1': {
                ... 'x1': {
                ... 'z1': 22,
                ... 'z2': 11,
                ... 'z3': 21,
                ... },
                ... 'x2': {
                ... 'z1': 15,
                ... 'z2': 34,
                ... 'z3': 54,
                ... }
                ... },
                ... 'test2': {
                ... 'x1': {
                ... 'z1': 22,
                ... 'z2': 11,
                ... 'z3': 21,
                ... },
                ... 'x2': {
                ... 'z1': 15,
                ... 'z2': 34,
                ... 'z3': 54,
                ... }
                ... }
                ... }
                >>>
                >>> d = data["test1"]
                >>> d
                {'x1': {'z1': 22, 'z2': 11, 'z3': 21}, 'x2': {'z1': 15, 'z2': 34, 'z3': 54}}
                >>>
                >>> df = pd.DataFrame(list(d.values()), index=list(d.keys()))
                >>> df
                z1 z2 z3
                x1 22 11 21
                x2 15 34 54
                >>>
                >>> df.z2.sum() # 1st way
                45
                >>>
                >>> df["z2"].sum() # 2nd way
                45
                >>>


                References »


                • https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.sum.html







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 10 at 16:31









                hygull

                2,70311126




                2,70311126






























                     

                    draft saved


                    draft discarded



















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53240766%2fhow-to-sum-values-from-all-objects-within-one-dictionary-in-python%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest




















































































                    Popular posts from this blog

                    Xamarin.iOS Cant Deploy on Iphone

                    Glorious Revolution

                    Dulmage-Mendelsohn matrix decomposition in Python