Pythonic way of executing a loop on a dictionary of lists









up vote
4
down vote

favorite












This code works but I was wondering if there is a more pythonic way for writing it.



word_frequency is a dictionary of lists, e.g.:



word_frequency = 'dogs': [1234, 4321], 'are': [9999, 0000], 'fun': [4389, 3234]

vocab_frequency = [0, 0] # stores the total times all the words used in each class
for word in word_frequency: # that is not the most elegant solution, but it works!
vocab_frequency[0] += word_frequency[word][0] #negative class
vocab_frequency[1] += word_frequency[word][1] #positive class


Is there a more elegant way of writing this loop?










share|improve this question



















  • 2




    Since you're not really using word except to look back into word_frequency you can iterate over word_frequency.values().
    – pdexter
    Nov 9 at 13:41










  • Lot's of simple and elegant answers here. So... I guess we might say "pythonic" means that the same thing can be done in a myriad of neat ways?
    – Karl
    Nov 9 at 21:52










  • @Karl python allows us to write very neat and concise code. However if, like me, you learned to program in C, many times you will catch yourself creating resource consuming loops and functions that could be handled in a much more elegant and often more efficient way. blog.startifact.com/posts/older/what-is-pythonic.html
    – Victor Zuanazzi
    Nov 10 at 14:20










  • Yee, my post was nothing more than a recognition of that. I was trying to point out if you ask a community of python programmers to come up the "the most pythonic way" (a question which I see all the time), what is revealed as pythonic is in fact that python allows for such a compact idiom in multiple ways
    – Karl
    Nov 10 at 17:33














up vote
4
down vote

favorite












This code works but I was wondering if there is a more pythonic way for writing it.



word_frequency is a dictionary of lists, e.g.:



word_frequency = 'dogs': [1234, 4321], 'are': [9999, 0000], 'fun': [4389, 3234]

vocab_frequency = [0, 0] # stores the total times all the words used in each class
for word in word_frequency: # that is not the most elegant solution, but it works!
vocab_frequency[0] += word_frequency[word][0] #negative class
vocab_frequency[1] += word_frequency[word][1] #positive class


Is there a more elegant way of writing this loop?










share|improve this question



















  • 2




    Since you're not really using word except to look back into word_frequency you can iterate over word_frequency.values().
    – pdexter
    Nov 9 at 13:41










  • Lot's of simple and elegant answers here. So... I guess we might say "pythonic" means that the same thing can be done in a myriad of neat ways?
    – Karl
    Nov 9 at 21:52










  • @Karl python allows us to write very neat and concise code. However if, like me, you learned to program in C, many times you will catch yourself creating resource consuming loops and functions that could be handled in a much more elegant and often more efficient way. blog.startifact.com/posts/older/what-is-pythonic.html
    – Victor Zuanazzi
    Nov 10 at 14:20










  • Yee, my post was nothing more than a recognition of that. I was trying to point out if you ask a community of python programmers to come up the "the most pythonic way" (a question which I see all the time), what is revealed as pythonic is in fact that python allows for such a compact idiom in multiple ways
    – Karl
    Nov 10 at 17:33












up vote
4
down vote

favorite









up vote
4
down vote

favorite











This code works but I was wondering if there is a more pythonic way for writing it.



word_frequency is a dictionary of lists, e.g.:



word_frequency = 'dogs': [1234, 4321], 'are': [9999, 0000], 'fun': [4389, 3234]

vocab_frequency = [0, 0] # stores the total times all the words used in each class
for word in word_frequency: # that is not the most elegant solution, but it works!
vocab_frequency[0] += word_frequency[word][0] #negative class
vocab_frequency[1] += word_frequency[word][1] #positive class


Is there a more elegant way of writing this loop?










share|improve this question















This code works but I was wondering if there is a more pythonic way for writing it.



word_frequency is a dictionary of lists, e.g.:



word_frequency = 'dogs': [1234, 4321], 'are': [9999, 0000], 'fun': [4389, 3234]

vocab_frequency = [0, 0] # stores the total times all the words used in each class
for word in word_frequency: # that is not the most elegant solution, but it works!
vocab_frequency[0] += word_frequency[word][0] #negative class
vocab_frequency[1] += word_frequency[word][1] #positive class


Is there a more elegant way of writing this loop?







python loops






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 9 at 13:40









Vlad

15.2k43162




15.2k43162










asked Nov 9 at 13:35









Victor Zuanazzi

6015




6015







  • 2




    Since you're not really using word except to look back into word_frequency you can iterate over word_frequency.values().
    – pdexter
    Nov 9 at 13:41










  • Lot's of simple and elegant answers here. So... I guess we might say "pythonic" means that the same thing can be done in a myriad of neat ways?
    – Karl
    Nov 9 at 21:52










  • @Karl python allows us to write very neat and concise code. However if, like me, you learned to program in C, many times you will catch yourself creating resource consuming loops and functions that could be handled in a much more elegant and often more efficient way. blog.startifact.com/posts/older/what-is-pythonic.html
    – Victor Zuanazzi
    Nov 10 at 14:20










  • Yee, my post was nothing more than a recognition of that. I was trying to point out if you ask a community of python programmers to come up the "the most pythonic way" (a question which I see all the time), what is revealed as pythonic is in fact that python allows for such a compact idiom in multiple ways
    – Karl
    Nov 10 at 17:33












  • 2




    Since you're not really using word except to look back into word_frequency you can iterate over word_frequency.values().
    – pdexter
    Nov 9 at 13:41










  • Lot's of simple and elegant answers here. So... I guess we might say "pythonic" means that the same thing can be done in a myriad of neat ways?
    – Karl
    Nov 9 at 21:52










  • @Karl python allows us to write very neat and concise code. However if, like me, you learned to program in C, many times you will catch yourself creating resource consuming loops and functions that could be handled in a much more elegant and often more efficient way. blog.startifact.com/posts/older/what-is-pythonic.html
    – Victor Zuanazzi
    Nov 10 at 14:20










  • Yee, my post was nothing more than a recognition of that. I was trying to point out if you ask a community of python programmers to come up the "the most pythonic way" (a question which I see all the time), what is revealed as pythonic is in fact that python allows for such a compact idiom in multiple ways
    – Karl
    Nov 10 at 17:33







2




2




Since you're not really using word except to look back into word_frequency you can iterate over word_frequency.values().
– pdexter
Nov 9 at 13:41




Since you're not really using word except to look back into word_frequency you can iterate over word_frequency.values().
– pdexter
Nov 9 at 13:41












Lot's of simple and elegant answers here. So... I guess we might say "pythonic" means that the same thing can be done in a myriad of neat ways?
– Karl
Nov 9 at 21:52




Lot's of simple and elegant answers here. So... I guess we might say "pythonic" means that the same thing can be done in a myriad of neat ways?
– Karl
Nov 9 at 21:52












@Karl python allows us to write very neat and concise code. However if, like me, you learned to program in C, many times you will catch yourself creating resource consuming loops and functions that could be handled in a much more elegant and often more efficient way. blog.startifact.com/posts/older/what-is-pythonic.html
– Victor Zuanazzi
Nov 10 at 14:20




@Karl python allows us to write very neat and concise code. However if, like me, you learned to program in C, many times you will catch yourself creating resource consuming loops and functions that could be handled in a much more elegant and often more efficient way. blog.startifact.com/posts/older/what-is-pythonic.html
– Victor Zuanazzi
Nov 10 at 14:20












Yee, my post was nothing more than a recognition of that. I was trying to point out if you ask a community of python programmers to come up the "the most pythonic way" (a question which I see all the time), what is revealed as pythonic is in fact that python allows for such a compact idiom in multiple ways
– Karl
Nov 10 at 17:33




Yee, my post was nothing more than a recognition of that. I was trying to point out if you ask a community of python programmers to come up the "the most pythonic way" (a question which I see all the time), what is revealed as pythonic is in fact that python allows for such a compact idiom in multiple ways
– Karl
Nov 10 at 17:33












9 Answers
9






active

oldest

votes

















up vote
4
down vote



accepted










You could use numpy for that:



import numpy as np

word_frequency = 'dogs': [1234, 4321], 'are': [9999, 0000], 'fun': [4389, 3234]
vocab_frequency = np.sum(list(word_frequency.values()), axis=0)





share|improve this answer
















  • 1




    Using numpy is like total over kill :P
    – SirtusKottus
    Nov 9 at 13:43










  • Why numpy...its -like Sirtuskottus said- overkill
    – NaruS
    Nov 9 at 13:44






  • 1




    True, it's overkill. But depending on the context you might have it imported already.
    – David Speck
    Nov 9 at 13:46

















up vote
8
down vote













I'm not sure if this is more Pythonic:



>>> word_frequency = 'dogs': [1234, 4321], 'are': [9999, 0000], 'fun': [4389, 3234]
>>> vocab_frequency = [sum(x[0] for x in word_frequency.values()),
sum(x[1] for x in word_frequency.values())]
>>> print(vocab_frequency)
[15622, 7555]


Alternate solution with reduce:



>>> reduce(lambda x, y: [x[0] + y[0], x[1] + y[1]], word_frequency.values())
[15622, 7555]





share|improve this answer




















  • A list comprehension over a for loop is more Pythonic in my eyes.
    – doctorlove
    Nov 9 at 13:45

















up vote
2
down vote













list(map(sum, zip(*word_frequency.values())))





share|improve this answer




















  • I think this is the shortest possible solution to the problem.
    – hochl
    Nov 9 at 13:51


















up vote
2
down vote













Probably not the shortest way to solve this, but hopefully the most comprehensible...



word_frequency = 'dogs': [1234, 4321], 'are': [9999, 0000], 'fun': [4389, 3234]

negative = (v[0] for v in word_frequency.values())
positive = (v[1] for v in word_frequency.values())
vocab_frequency = sum(negative), sum(positive)

print (vocab_frequency) # (15622, 7555)


Though more experienced Pythonistas might rather use zip to unpack the values:



negative, positive = zip(*word_frequency.values())
vocab_frequency = sum(negative), sum(positive)





share|improve this answer





























    up vote
    1
    down vote













    for frequencies in word_frequency.values():
    vocab_frequency = [sum(x) for x in zip(vocab_frequency, frequencies)]





    share|improve this answer



























      up vote
      1
      down vote













      Another approach would be this:



      vocab_frequency[0], vocab_frequency[1] = list(sum([word_frequency[elem][i] for elem in word_frequency]) for i in range(2))

      print(vocab_frequency[0])
      print(vocab_frequency[1])


      Output:



      15622
      7555


      Still, one more way to do it, kind of far fetched is this:



      *vocab_frequency, = list(map(sum,zip(*word_frequency.values())))

      print(vocab_frequency)


      Output:



      [15622, 7555]





      share|improve this answer





























        up vote
        1
        down vote













        You can convert that dictionary to a pandas DataFrame and it would be a lot easier to handle.



        import pandas as pd
        word_frequency = 'dogs': [1234, 4321], 'are': [9999, 0000], 'fun': [4389, 3234]

        #Syntax to create DataFrame
        df = pd.DataFrame(word_frequency)

        #Result
        dogs are fun
        0 1234 9999 4389
        1 4321 0 3234


        Now just take the sum of each row and either convert back to list or keep as a dataframe object.



        #Take sum of each row and convert to list
        df = df.sum(axis=1)
        df = df.values.tolist()
        print(df)

        #Output
        [15622, 7555]





        share|improve this answer








        New contributor




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
























          up vote
          0
          down vote













          Try This Single Line Solution:



          [sum([word_frequency[i][0] for i in word_frequency]),sum([word_frequency[i][1] for i in word_frequency])]





          share|improve this answer



























            up vote
            0
            down vote













            for n, p in your_dict.vales():
            res[0] += n
            res[1] += p


            This will be fast and elegant enough.
            Sent from phone. Sorry for the format.






            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%2f53226715%2fpythonic-way-of-executing-a-loop-on-a-dictionary-of-lists%23new-answer', 'question_page');

              );

              Post as a guest






























              9 Answers
              9






              active

              oldest

              votes








              9 Answers
              9






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              4
              down vote



              accepted










              You could use numpy for that:



              import numpy as np

              word_frequency = 'dogs': [1234, 4321], 'are': [9999, 0000], 'fun': [4389, 3234]
              vocab_frequency = np.sum(list(word_frequency.values()), axis=0)





              share|improve this answer
















              • 1




                Using numpy is like total over kill :P
                – SirtusKottus
                Nov 9 at 13:43










              • Why numpy...its -like Sirtuskottus said- overkill
                – NaruS
                Nov 9 at 13:44






              • 1




                True, it's overkill. But depending on the context you might have it imported already.
                – David Speck
                Nov 9 at 13:46














              up vote
              4
              down vote



              accepted










              You could use numpy for that:



              import numpy as np

              word_frequency = 'dogs': [1234, 4321], 'are': [9999, 0000], 'fun': [4389, 3234]
              vocab_frequency = np.sum(list(word_frequency.values()), axis=0)





              share|improve this answer
















              • 1




                Using numpy is like total over kill :P
                – SirtusKottus
                Nov 9 at 13:43










              • Why numpy...its -like Sirtuskottus said- overkill
                – NaruS
                Nov 9 at 13:44






              • 1




                True, it's overkill. But depending on the context you might have it imported already.
                – David Speck
                Nov 9 at 13:46












              up vote
              4
              down vote



              accepted







              up vote
              4
              down vote



              accepted






              You could use numpy for that:



              import numpy as np

              word_frequency = 'dogs': [1234, 4321], 'are': [9999, 0000], 'fun': [4389, 3234]
              vocab_frequency = np.sum(list(word_frequency.values()), axis=0)





              share|improve this answer












              You could use numpy for that:



              import numpy as np

              word_frequency = 'dogs': [1234, 4321], 'are': [9999, 0000], 'fun': [4389, 3234]
              vocab_frequency = np.sum(list(word_frequency.values()), axis=0)






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Nov 9 at 13:42









              David Speck

              1586




              1586







              • 1




                Using numpy is like total over kill :P
                – SirtusKottus
                Nov 9 at 13:43










              • Why numpy...its -like Sirtuskottus said- overkill
                – NaruS
                Nov 9 at 13:44






              • 1




                True, it's overkill. But depending on the context you might have it imported already.
                – David Speck
                Nov 9 at 13:46












              • 1




                Using numpy is like total over kill :P
                – SirtusKottus
                Nov 9 at 13:43










              • Why numpy...its -like Sirtuskottus said- overkill
                – NaruS
                Nov 9 at 13:44






              • 1




                True, it's overkill. But depending on the context you might have it imported already.
                – David Speck
                Nov 9 at 13:46







              1




              1




              Using numpy is like total over kill :P
              – SirtusKottus
              Nov 9 at 13:43




              Using numpy is like total over kill :P
              – SirtusKottus
              Nov 9 at 13:43












              Why numpy...its -like Sirtuskottus said- overkill
              – NaruS
              Nov 9 at 13:44




              Why numpy...its -like Sirtuskottus said- overkill
              – NaruS
              Nov 9 at 13:44




              1




              1




              True, it's overkill. But depending on the context you might have it imported already.
              – David Speck
              Nov 9 at 13:46




              True, it's overkill. But depending on the context you might have it imported already.
              – David Speck
              Nov 9 at 13:46












              up vote
              8
              down vote













              I'm not sure if this is more Pythonic:



              >>> word_frequency = 'dogs': [1234, 4321], 'are': [9999, 0000], 'fun': [4389, 3234]
              >>> vocab_frequency = [sum(x[0] for x in word_frequency.values()),
              sum(x[1] for x in word_frequency.values())]
              >>> print(vocab_frequency)
              [15622, 7555]


              Alternate solution with reduce:



              >>> reduce(lambda x, y: [x[0] + y[0], x[1] + y[1]], word_frequency.values())
              [15622, 7555]





              share|improve this answer




















              • A list comprehension over a for loop is more Pythonic in my eyes.
                – doctorlove
                Nov 9 at 13:45














              up vote
              8
              down vote













              I'm not sure if this is more Pythonic:



              >>> word_frequency = 'dogs': [1234, 4321], 'are': [9999, 0000], 'fun': [4389, 3234]
              >>> vocab_frequency = [sum(x[0] for x in word_frequency.values()),
              sum(x[1] for x in word_frequency.values())]
              >>> print(vocab_frequency)
              [15622, 7555]


              Alternate solution with reduce:



              >>> reduce(lambda x, y: [x[0] + y[0], x[1] + y[1]], word_frequency.values())
              [15622, 7555]





              share|improve this answer




















              • A list comprehension over a for loop is more Pythonic in my eyes.
                – doctorlove
                Nov 9 at 13:45












              up vote
              8
              down vote










              up vote
              8
              down vote









              I'm not sure if this is more Pythonic:



              >>> word_frequency = 'dogs': [1234, 4321], 'are': [9999, 0000], 'fun': [4389, 3234]
              >>> vocab_frequency = [sum(x[0] for x in word_frequency.values()),
              sum(x[1] for x in word_frequency.values())]
              >>> print(vocab_frequency)
              [15622, 7555]


              Alternate solution with reduce:



              >>> reduce(lambda x, y: [x[0] + y[0], x[1] + y[1]], word_frequency.values())
              [15622, 7555]





              share|improve this answer












              I'm not sure if this is more Pythonic:



              >>> word_frequency = 'dogs': [1234, 4321], 'are': [9999, 0000], 'fun': [4389, 3234]
              >>> vocab_frequency = [sum(x[0] for x in word_frequency.values()),
              sum(x[1] for x in word_frequency.values())]
              >>> print(vocab_frequency)
              [15622, 7555]


              Alternate solution with reduce:



              >>> reduce(lambda x, y: [x[0] + y[0], x[1] + y[1]], word_frequency.values())
              [15622, 7555]






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Nov 9 at 13:41









              hochl

              8,78273367




              8,78273367











              • A list comprehension over a for loop is more Pythonic in my eyes.
                – doctorlove
                Nov 9 at 13:45
















              • A list comprehension over a for loop is more Pythonic in my eyes.
                – doctorlove
                Nov 9 at 13:45















              A list comprehension over a for loop is more Pythonic in my eyes.
              – doctorlove
              Nov 9 at 13:45




              A list comprehension over a for loop is more Pythonic in my eyes.
              – doctorlove
              Nov 9 at 13:45










              up vote
              2
              down vote













              list(map(sum, zip(*word_frequency.values())))





              share|improve this answer




















              • I think this is the shortest possible solution to the problem.
                – hochl
                Nov 9 at 13:51















              up vote
              2
              down vote













              list(map(sum, zip(*word_frequency.values())))





              share|improve this answer




















              • I think this is the shortest possible solution to the problem.
                – hochl
                Nov 9 at 13:51













              up vote
              2
              down vote










              up vote
              2
              down vote









              list(map(sum, zip(*word_frequency.values())))





              share|improve this answer












              list(map(sum, zip(*word_frequency.values())))






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Nov 9 at 13:49









              drhodes

              635814




              635814











              • I think this is the shortest possible solution to the problem.
                – hochl
                Nov 9 at 13:51

















              • I think this is the shortest possible solution to the problem.
                – hochl
                Nov 9 at 13:51
















              I think this is the shortest possible solution to the problem.
              – hochl
              Nov 9 at 13:51





              I think this is the shortest possible solution to the problem.
              – hochl
              Nov 9 at 13:51











              up vote
              2
              down vote













              Probably not the shortest way to solve this, but hopefully the most comprehensible...



              word_frequency = 'dogs': [1234, 4321], 'are': [9999, 0000], 'fun': [4389, 3234]

              negative = (v[0] for v in word_frequency.values())
              positive = (v[1] for v in word_frequency.values())
              vocab_frequency = sum(negative), sum(positive)

              print (vocab_frequency) # (15622, 7555)


              Though more experienced Pythonistas might rather use zip to unpack the values:



              negative, positive = zip(*word_frequency.values())
              vocab_frequency = sum(negative), sum(positive)





              share|improve this answer


























                up vote
                2
                down vote













                Probably not the shortest way to solve this, but hopefully the most comprehensible...



                word_frequency = 'dogs': [1234, 4321], 'are': [9999, 0000], 'fun': [4389, 3234]

                negative = (v[0] for v in word_frequency.values())
                positive = (v[1] for v in word_frequency.values())
                vocab_frequency = sum(negative), sum(positive)

                print (vocab_frequency) # (15622, 7555)


                Though more experienced Pythonistas might rather use zip to unpack the values:



                negative, positive = zip(*word_frequency.values())
                vocab_frequency = sum(negative), sum(positive)





                share|improve this answer
























                  up vote
                  2
                  down vote










                  up vote
                  2
                  down vote









                  Probably not the shortest way to solve this, but hopefully the most comprehensible...



                  word_frequency = 'dogs': [1234, 4321], 'are': [9999, 0000], 'fun': [4389, 3234]

                  negative = (v[0] for v in word_frequency.values())
                  positive = (v[1] for v in word_frequency.values())
                  vocab_frequency = sum(negative), sum(positive)

                  print (vocab_frequency) # (15622, 7555)


                  Though more experienced Pythonistas might rather use zip to unpack the values:



                  negative, positive = zip(*word_frequency.values())
                  vocab_frequency = sum(negative), sum(positive)





                  share|improve this answer














                  Probably not the shortest way to solve this, but hopefully the most comprehensible...



                  word_frequency = 'dogs': [1234, 4321], 'are': [9999, 0000], 'fun': [4389, 3234]

                  negative = (v[0] for v in word_frequency.values())
                  positive = (v[1] for v in word_frequency.values())
                  vocab_frequency = sum(negative), sum(positive)

                  print (vocab_frequency) # (15622, 7555)


                  Though more experienced Pythonistas might rather use zip to unpack the values:



                  negative, positive = zip(*word_frequency.values())
                  vocab_frequency = sum(negative), sum(positive)






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 9 at 14:00

























                  answered Nov 9 at 13:50









                  Sebastian Loehner

                  59735




                  59735




















                      up vote
                      1
                      down vote













                      for frequencies in word_frequency.values():
                      vocab_frequency = [sum(x) for x in zip(vocab_frequency, frequencies)]





                      share|improve this answer
























                        up vote
                        1
                        down vote













                        for frequencies in word_frequency.values():
                        vocab_frequency = [sum(x) for x in zip(vocab_frequency, frequencies)]





                        share|improve this answer






















                          up vote
                          1
                          down vote










                          up vote
                          1
                          down vote









                          for frequencies in word_frequency.values():
                          vocab_frequency = [sum(x) for x in zip(vocab_frequency, frequencies)]





                          share|improve this answer












                          for frequencies in word_frequency.values():
                          vocab_frequency = [sum(x) for x in zip(vocab_frequency, frequencies)]






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 9 at 13:46









                          Karl

                          1,28842649




                          1,28842649




















                              up vote
                              1
                              down vote













                              Another approach would be this:



                              vocab_frequency[0], vocab_frequency[1] = list(sum([word_frequency[elem][i] for elem in word_frequency]) for i in range(2))

                              print(vocab_frequency[0])
                              print(vocab_frequency[1])


                              Output:



                              15622
                              7555


                              Still, one more way to do it, kind of far fetched is this:



                              *vocab_frequency, = list(map(sum,zip(*word_frequency.values())))

                              print(vocab_frequency)


                              Output:



                              [15622, 7555]





                              share|improve this answer


























                                up vote
                                1
                                down vote













                                Another approach would be this:



                                vocab_frequency[0], vocab_frequency[1] = list(sum([word_frequency[elem][i] for elem in word_frequency]) for i in range(2))

                                print(vocab_frequency[0])
                                print(vocab_frequency[1])


                                Output:



                                15622
                                7555


                                Still, one more way to do it, kind of far fetched is this:



                                *vocab_frequency, = list(map(sum,zip(*word_frequency.values())))

                                print(vocab_frequency)


                                Output:



                                [15622, 7555]





                                share|improve this answer
























                                  up vote
                                  1
                                  down vote










                                  up vote
                                  1
                                  down vote









                                  Another approach would be this:



                                  vocab_frequency[0], vocab_frequency[1] = list(sum([word_frequency[elem][i] for elem in word_frequency]) for i in range(2))

                                  print(vocab_frequency[0])
                                  print(vocab_frequency[1])


                                  Output:



                                  15622
                                  7555


                                  Still, one more way to do it, kind of far fetched is this:



                                  *vocab_frequency, = list(map(sum,zip(*word_frequency.values())))

                                  print(vocab_frequency)


                                  Output:



                                  [15622, 7555]





                                  share|improve this answer














                                  Another approach would be this:



                                  vocab_frequency[0], vocab_frequency[1] = list(sum([word_frequency[elem][i] for elem in word_frequency]) for i in range(2))

                                  print(vocab_frequency[0])
                                  print(vocab_frequency[1])


                                  Output:



                                  15622
                                  7555


                                  Still, one more way to do it, kind of far fetched is this:



                                  *vocab_frequency, = list(map(sum,zip(*word_frequency.values())))

                                  print(vocab_frequency)


                                  Output:



                                  [15622, 7555]






                                  share|improve this answer














                                  share|improve this answer



                                  share|improve this answer








                                  edited Nov 9 at 13:52

























                                  answered Nov 9 at 13:44









                                  Vasilis G.

                                  2,5892621




                                  2,5892621




















                                      up vote
                                      1
                                      down vote













                                      You can convert that dictionary to a pandas DataFrame and it would be a lot easier to handle.



                                      import pandas as pd
                                      word_frequency = 'dogs': [1234, 4321], 'are': [9999, 0000], 'fun': [4389, 3234]

                                      #Syntax to create DataFrame
                                      df = pd.DataFrame(word_frequency)

                                      #Result
                                      dogs are fun
                                      0 1234 9999 4389
                                      1 4321 0 3234


                                      Now just take the sum of each row and either convert back to list or keep as a dataframe object.



                                      #Take sum of each row and convert to list
                                      df = df.sum(axis=1)
                                      df = df.values.tolist()
                                      print(df)

                                      #Output
                                      [15622, 7555]





                                      share|improve this answer








                                      New contributor




                                      Drew Nicolette 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













                                        You can convert that dictionary to a pandas DataFrame and it would be a lot easier to handle.



                                        import pandas as pd
                                        word_frequency = 'dogs': [1234, 4321], 'are': [9999, 0000], 'fun': [4389, 3234]

                                        #Syntax to create DataFrame
                                        df = pd.DataFrame(word_frequency)

                                        #Result
                                        dogs are fun
                                        0 1234 9999 4389
                                        1 4321 0 3234


                                        Now just take the sum of each row and either convert back to list or keep as a dataframe object.



                                        #Take sum of each row and convert to list
                                        df = df.sum(axis=1)
                                        df = df.values.tolist()
                                        print(df)

                                        #Output
                                        [15622, 7555]





                                        share|improve this answer








                                        New contributor




                                        Drew Nicolette 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










                                          up vote
                                          1
                                          down vote









                                          You can convert that dictionary to a pandas DataFrame and it would be a lot easier to handle.



                                          import pandas as pd
                                          word_frequency = 'dogs': [1234, 4321], 'are': [9999, 0000], 'fun': [4389, 3234]

                                          #Syntax to create DataFrame
                                          df = pd.DataFrame(word_frequency)

                                          #Result
                                          dogs are fun
                                          0 1234 9999 4389
                                          1 4321 0 3234


                                          Now just take the sum of each row and either convert back to list or keep as a dataframe object.



                                          #Take sum of each row and convert to list
                                          df = df.sum(axis=1)
                                          df = df.values.tolist()
                                          print(df)

                                          #Output
                                          [15622, 7555]





                                          share|improve this answer








                                          New contributor




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









                                          You can convert that dictionary to a pandas DataFrame and it would be a lot easier to handle.



                                          import pandas as pd
                                          word_frequency = 'dogs': [1234, 4321], 'are': [9999, 0000], 'fun': [4389, 3234]

                                          #Syntax to create DataFrame
                                          df = pd.DataFrame(word_frequency)

                                          #Result
                                          dogs are fun
                                          0 1234 9999 4389
                                          1 4321 0 3234


                                          Now just take the sum of each row and either convert back to list or keep as a dataframe object.



                                          #Take sum of each row and convert to list
                                          df = df.sum(axis=1)
                                          df = df.values.tolist()
                                          print(df)

                                          #Output
                                          [15622, 7555]






                                          share|improve this answer








                                          New contributor




                                          Drew Nicolette 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 answer



                                          share|improve this answer






                                          New contributor




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









                                          answered Nov 9 at 13:53









                                          Drew Nicolette

                                          963




                                          963




                                          New contributor




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





                                          New contributor





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






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




















                                              up vote
                                              0
                                              down vote













                                              Try This Single Line Solution:



                                              [sum([word_frequency[i][0] for i in word_frequency]),sum([word_frequency[i][1] for i in word_frequency])]





                                              share|improve this answer
























                                                up vote
                                                0
                                                down vote













                                                Try This Single Line Solution:



                                                [sum([word_frequency[i][0] for i in word_frequency]),sum([word_frequency[i][1] for i in word_frequency])]





                                                share|improve this answer






















                                                  up vote
                                                  0
                                                  down vote










                                                  up vote
                                                  0
                                                  down vote









                                                  Try This Single Line Solution:



                                                  [sum([word_frequency[i][0] for i in word_frequency]),sum([word_frequency[i][1] for i in word_frequency])]





                                                  share|improve this answer












                                                  Try This Single Line Solution:



                                                  [sum([word_frequency[i][0] for i in word_frequency]),sum([word_frequency[i][1] for i in word_frequency])]






                                                  share|improve this answer












                                                  share|improve this answer



                                                  share|improve this answer










                                                  answered Nov 9 at 14:06









                                                  Rohit-Pandey

                                                  888613




                                                  888613




















                                                      up vote
                                                      0
                                                      down vote













                                                      for n, p in your_dict.vales():
                                                      res[0] += n
                                                      res[1] += p


                                                      This will be fast and elegant enough.
                                                      Sent from phone. Sorry for the format.






                                                      share|improve this answer
























                                                        up vote
                                                        0
                                                        down vote













                                                        for n, p in your_dict.vales():
                                                        res[0] += n
                                                        res[1] += p


                                                        This will be fast and elegant enough.
                                                        Sent from phone. Sorry for the format.






                                                        share|improve this answer






















                                                          up vote
                                                          0
                                                          down vote










                                                          up vote
                                                          0
                                                          down vote









                                                          for n, p in your_dict.vales():
                                                          res[0] += n
                                                          res[1] += p


                                                          This will be fast and elegant enough.
                                                          Sent from phone. Sorry for the format.






                                                          share|improve this answer












                                                          for n, p in your_dict.vales():
                                                          res[0] += n
                                                          res[1] += p


                                                          This will be fast and elegant enough.
                                                          Sent from phone. Sorry for the format.







                                                          share|improve this answer












                                                          share|improve this answer



                                                          share|improve this answer










                                                          answered Nov 9 at 14:11









                                                          fakeKmz

                                                          146




                                                          146



























                                                               

                                                              draft saved


                                                              draft discarded















































                                                               


                                                              draft saved


                                                              draft discarded














                                                              StackExchange.ready(
                                                              function ()
                                                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53226715%2fpythonic-way-of-executing-a-loop-on-a-dictionary-of-lists%23new-answer', 'question_page');

                                                              );

                                                              Post as a guest














































































                                                              Popular posts from this blog

                                                              Darth Vader #20

                                                              How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

                                                              Ondo