Search in a nested list of a dictionary

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
1
down vote

favorite












I have a dictionary structured as a_1: [((b_11,c_11),d_11), ((b_12, c_12), d_12), ...], a_2: [...], ....



I want to extract the maximum abs(c) among all the possible c values. Currently I'm doing this throught two for loops. One for each a, and other for each of set b,c,d.



elements = 
0: [((0, -4), 1)],
1: [((1, -5), 1), ((0, -4), 1)],
2: [((2, -3), 1)],
3: [((3, -2), 1), ((0, -5), 1)],


max_c = 0

for a in list(elements):
for (b, c), d in elements[a]:
max_c = max(max_c, abs(c))


Is there any way to do this without the for loops? and if it is, is it pythonic? or I should keep this way because it's more understandable?



Thanks in advance.










share|improve this question









New contributor




Xbel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.























    up vote
    1
    down vote

    favorite












    I have a dictionary structured as a_1: [((b_11,c_11),d_11), ((b_12, c_12), d_12), ...], a_2: [...], ....



    I want to extract the maximum abs(c) among all the possible c values. Currently I'm doing this throught two for loops. One for each a, and other for each of set b,c,d.



    elements = 
    0: [((0, -4), 1)],
    1: [((1, -5), 1), ((0, -4), 1)],
    2: [((2, -3), 1)],
    3: [((3, -2), 1), ((0, -5), 1)],


    max_c = 0

    for a in list(elements):
    for (b, c), d in elements[a]:
    max_c = max(max_c, abs(c))


    Is there any way to do this without the for loops? and if it is, is it pythonic? or I should keep this way because it's more understandable?



    Thanks in advance.










    share|improve this question









    New contributor




    Xbel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.





















      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I have a dictionary structured as a_1: [((b_11,c_11),d_11), ((b_12, c_12), d_12), ...], a_2: [...], ....



      I want to extract the maximum abs(c) among all the possible c values. Currently I'm doing this throught two for loops. One for each a, and other for each of set b,c,d.



      elements = 
      0: [((0, -4), 1)],
      1: [((1, -5), 1), ((0, -4), 1)],
      2: [((2, -3), 1)],
      3: [((3, -2), 1), ((0, -5), 1)],


      max_c = 0

      for a in list(elements):
      for (b, c), d in elements[a]:
      max_c = max(max_c, abs(c))


      Is there any way to do this without the for loops? and if it is, is it pythonic? or I should keep this way because it's more understandable?



      Thanks in advance.










      share|improve this question









      New contributor




      Xbel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      I have a dictionary structured as a_1: [((b_11,c_11),d_11), ((b_12, c_12), d_12), ...], a_2: [...], ....



      I want to extract the maximum abs(c) among all the possible c values. Currently I'm doing this throught two for loops. One for each a, and other for each of set b,c,d.



      elements = 
      0: [((0, -4), 1)],
      1: [((1, -5), 1), ((0, -4), 1)],
      2: [((2, -3), 1)],
      3: [((3, -2), 1), ((0, -5), 1)],


      max_c = 0

      for a in list(elements):
      for (b, c), d in elements[a]:
      max_c = max(max_c, abs(c))


      Is there any way to do this without the for loops? and if it is, is it pythonic? or I should keep this way because it's more understandable?



      Thanks in advance.







      python dictionary






      share|improve this question









      New contributor




      Xbel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question









      New contributor




      Xbel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question








      edited 3 hours ago





















      New contributor




      Xbel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 3 hours ago









      Xbel

      235




      235




      New contributor




      Xbel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      Xbel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      Xbel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          2
          down vote













          If you were to extract all abs(c) into a list, you could use max over that list.



          Fortunately, this is easy using your loops and a list-comprehension:



          Cs = [
          abs(value)
          for collection in elements.values()
          for (_, value), _ in collection
          ]
          max_c = max(Cs)


          Note the use of elements.values() instead of extracting the values manually (elements[a]) and the use of _ as a throwaway variable.



          Now max works on any iterable, meaning we don't even have to build a temporary list and can feed it a generator-expression:



          max_c = max(
          abs(value)
          for collection in elements.values()
          for (_, value), _ in collection
          )





          share|improve this answer



























            up vote
            2
            down vote














            1. When you are looping over the keys a and values elements[a] of a dictionary, use the items method to loop over both at the same time:



              for a, value in elements.items():
              for (b, c), d in value:


              (If I knew what these values represented, I would pick a better name than value.)




            2. The code here doesn't use the keys, it only uses the values. So use the values method instead:



              for value in elements.values():
              for (b, c), d in value:



            3. The code doesn't use b or d either. It is conventional to use the name _ for unused variables:



              for (_, c), _ in value:



            4. A double iteration over some lists and then over the elements of those lists can be combined into a single iteration using itertools.chain.from_iterable:



              from itertools import chain
              for (_, c), _ in chain.from_iterable(elements.values()):



            5. The repeated calls to max can become a single call taking a generator expression:



              values = chain.from_iterable(elements.values())
              max_abs_c = max(abs(c) for (_, c), _ in values)


            6. It's common to worry whether code is "Pythonic" but it is better to think in terms of general principles (clarity, simplicity, maintainability, testability, usability, efficiency, etc.) that apply to code in any language.






            share|improve this answer






















              Your Answer




              StackExchange.ifUsing("editor", function ()
              return StackExchange.using("mathjaxEditing", function ()
              StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
              StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
              );
              );
              , "mathjax-editing");

              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: "196"
              ;
              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: false,
              noModals: false,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: null,
              bindNavPrevention: true,
              postfix: "",
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              );



              );






              Xbel is a new contributor. Be nice, and check out our Code of Conduct.









               

              draft saved


              draft discarded


















              StackExchange.ready(
              function ()
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f205733%2fsearch-in-a-nested-list-of-a-dictionary%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
              2
              down vote













              If you were to extract all abs(c) into a list, you could use max over that list.



              Fortunately, this is easy using your loops and a list-comprehension:



              Cs = [
              abs(value)
              for collection in elements.values()
              for (_, value), _ in collection
              ]
              max_c = max(Cs)


              Note the use of elements.values() instead of extracting the values manually (elements[a]) and the use of _ as a throwaway variable.



              Now max works on any iterable, meaning we don't even have to build a temporary list and can feed it a generator-expression:



              max_c = max(
              abs(value)
              for collection in elements.values()
              for (_, value), _ in collection
              )





              share|improve this answer
























                up vote
                2
                down vote













                If you were to extract all abs(c) into a list, you could use max over that list.



                Fortunately, this is easy using your loops and a list-comprehension:



                Cs = [
                abs(value)
                for collection in elements.values()
                for (_, value), _ in collection
                ]
                max_c = max(Cs)


                Note the use of elements.values() instead of extracting the values manually (elements[a]) and the use of _ as a throwaway variable.



                Now max works on any iterable, meaning we don't even have to build a temporary list and can feed it a generator-expression:



                max_c = max(
                abs(value)
                for collection in elements.values()
                for (_, value), _ in collection
                )





                share|improve this answer






















                  up vote
                  2
                  down vote










                  up vote
                  2
                  down vote









                  If you were to extract all abs(c) into a list, you could use max over that list.



                  Fortunately, this is easy using your loops and a list-comprehension:



                  Cs = [
                  abs(value)
                  for collection in elements.values()
                  for (_, value), _ in collection
                  ]
                  max_c = max(Cs)


                  Note the use of elements.values() instead of extracting the values manually (elements[a]) and the use of _ as a throwaway variable.



                  Now max works on any iterable, meaning we don't even have to build a temporary list and can feed it a generator-expression:



                  max_c = max(
                  abs(value)
                  for collection in elements.values()
                  for (_, value), _ in collection
                  )





                  share|improve this answer












                  If you were to extract all abs(c) into a list, you could use max over that list.



                  Fortunately, this is easy using your loops and a list-comprehension:



                  Cs = [
                  abs(value)
                  for collection in elements.values()
                  for (_, value), _ in collection
                  ]
                  max_c = max(Cs)


                  Note the use of elements.values() instead of extracting the values manually (elements[a]) and the use of _ as a throwaway variable.



                  Now max works on any iterable, meaning we don't even have to build a temporary list and can feed it a generator-expression:



                  max_c = max(
                  abs(value)
                  for collection in elements.values()
                  for (_, value), _ in collection
                  )






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 30 mins ago









                  Mathias Ettinger

                  22.5k33077




                  22.5k33077






















                      up vote
                      2
                      down vote














                      1. When you are looping over the keys a and values elements[a] of a dictionary, use the items method to loop over both at the same time:



                        for a, value in elements.items():
                        for (b, c), d in value:


                        (If I knew what these values represented, I would pick a better name than value.)




                      2. The code here doesn't use the keys, it only uses the values. So use the values method instead:



                        for value in elements.values():
                        for (b, c), d in value:



                      3. The code doesn't use b or d either. It is conventional to use the name _ for unused variables:



                        for (_, c), _ in value:



                      4. A double iteration over some lists and then over the elements of those lists can be combined into a single iteration using itertools.chain.from_iterable:



                        from itertools import chain
                        for (_, c), _ in chain.from_iterable(elements.values()):



                      5. The repeated calls to max can become a single call taking a generator expression:



                        values = chain.from_iterable(elements.values())
                        max_abs_c = max(abs(c) for (_, c), _ in values)


                      6. It's common to worry whether code is "Pythonic" but it is better to think in terms of general principles (clarity, simplicity, maintainability, testability, usability, efficiency, etc.) that apply to code in any language.






                      share|improve this answer


























                        up vote
                        2
                        down vote














                        1. When you are looping over the keys a and values elements[a] of a dictionary, use the items method to loop over both at the same time:



                          for a, value in elements.items():
                          for (b, c), d in value:


                          (If I knew what these values represented, I would pick a better name than value.)




                        2. The code here doesn't use the keys, it only uses the values. So use the values method instead:



                          for value in elements.values():
                          for (b, c), d in value:



                        3. The code doesn't use b or d either. It is conventional to use the name _ for unused variables:



                          for (_, c), _ in value:



                        4. A double iteration over some lists and then over the elements of those lists can be combined into a single iteration using itertools.chain.from_iterable:



                          from itertools import chain
                          for (_, c), _ in chain.from_iterable(elements.values()):



                        5. The repeated calls to max can become a single call taking a generator expression:



                          values = chain.from_iterable(elements.values())
                          max_abs_c = max(abs(c) for (_, c), _ in values)


                        6. It's common to worry whether code is "Pythonic" but it is better to think in terms of general principles (clarity, simplicity, maintainability, testability, usability, efficiency, etc.) that apply to code in any language.






                        share|improve this answer
























                          up vote
                          2
                          down vote










                          up vote
                          2
                          down vote










                          1. When you are looping over the keys a and values elements[a] of a dictionary, use the items method to loop over both at the same time:



                            for a, value in elements.items():
                            for (b, c), d in value:


                            (If I knew what these values represented, I would pick a better name than value.)




                          2. The code here doesn't use the keys, it only uses the values. So use the values method instead:



                            for value in elements.values():
                            for (b, c), d in value:



                          3. The code doesn't use b or d either. It is conventional to use the name _ for unused variables:



                            for (_, c), _ in value:



                          4. A double iteration over some lists and then over the elements of those lists can be combined into a single iteration using itertools.chain.from_iterable:



                            from itertools import chain
                            for (_, c), _ in chain.from_iterable(elements.values()):



                          5. The repeated calls to max can become a single call taking a generator expression:



                            values = chain.from_iterable(elements.values())
                            max_abs_c = max(abs(c) for (_, c), _ in values)


                          6. It's common to worry whether code is "Pythonic" but it is better to think in terms of general principles (clarity, simplicity, maintainability, testability, usability, efficiency, etc.) that apply to code in any language.






                          share|improve this answer















                          1. When you are looping over the keys a and values elements[a] of a dictionary, use the items method to loop over both at the same time:



                            for a, value in elements.items():
                            for (b, c), d in value:


                            (If I knew what these values represented, I would pick a better name than value.)




                          2. The code here doesn't use the keys, it only uses the values. So use the values method instead:



                            for value in elements.values():
                            for (b, c), d in value:



                          3. The code doesn't use b or d either. It is conventional to use the name _ for unused variables:



                            for (_, c), _ in value:



                          4. A double iteration over some lists and then over the elements of those lists can be combined into a single iteration using itertools.chain.from_iterable:



                            from itertools import chain
                            for (_, c), _ in chain.from_iterable(elements.values()):



                          5. The repeated calls to max can become a single call taking a generator expression:



                            values = chain.from_iterable(elements.values())
                            max_abs_c = max(abs(c) for (_, c), _ in values)


                          6. It's common to worry whether code is "Pythonic" but it is better to think in terms of general principles (clarity, simplicity, maintainability, testability, usability, efficiency, etc.) that apply to code in any language.







                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited 24 mins ago

























                          answered 29 mins ago









                          Gareth Rees

                          43.5k396175




                          43.5k396175




















                              Xbel is a new contributor. Be nice, and check out our Code of Conduct.









                               

                              draft saved


                              draft discarded


















                              Xbel is a new contributor. Be nice, and check out our Code of Conduct.












                              Xbel is a new contributor. Be nice, and check out our Code of Conduct.











                              Xbel is a new contributor. Be nice, and check out our Code of Conduct.













                               


                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f205733%2fsearch-in-a-nested-list-of-a-dictionary%23new-answer', 'question_page');

                              );

                              Post as a guest













































































                              Comments

                              Popular posts from this blog

                              What does second last employer means? [closed]

                              List of Gilmore Girls characters

                              Confectionery