Nested dict key overwritten











up vote
0
down vote

favorite












I want to create a nested dict through a for loop. All the customers and categories keys are created as expected (i and j), but for the product_categories only the last key is written. I know it's probably getting overwritten, but I can't understand why?



top_10_recs = {}

for i, j, k in itertools.product(customers, categories, product_categories):
if i not in top_10_recs:
top_10_recs[i] = {}
if j not in top_10_recs:
top_10_recs[i][j] = {}
if k not in top_10_recs:
top_10_recs[i][j][k] = {}
try:
top_10_recs[i][j][k] = trained_dataframe.loc[(i, j), k].nlargest(10).to_dict()
except:
pass


If I do a print with this:



for i, j, k in itertools.product(customers, categories, product_categories):
try:
print("{}".format(k))
print(trained_dataframe.loc[(i, j), k].nlargest(10))


I get the expected result, looping through all product_categories.










share|improve this question


























    up vote
    0
    down vote

    favorite












    I want to create a nested dict through a for loop. All the customers and categories keys are created as expected (i and j), but for the product_categories only the last key is written. I know it's probably getting overwritten, but I can't understand why?



    top_10_recs = {}

    for i, j, k in itertools.product(customers, categories, product_categories):
    if i not in top_10_recs:
    top_10_recs[i] = {}
    if j not in top_10_recs:
    top_10_recs[i][j] = {}
    if k not in top_10_recs:
    top_10_recs[i][j][k] = {}
    try:
    top_10_recs[i][j][k] = trained_dataframe.loc[(i, j), k].nlargest(10).to_dict()
    except:
    pass


    If I do a print with this:



    for i, j, k in itertools.product(customers, categories, product_categories):
    try:
    print("{}".format(k))
    print(trained_dataframe.loc[(i, j), k].nlargest(10))


    I get the expected result, looping through all product_categories.










    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I want to create a nested dict through a for loop. All the customers and categories keys are created as expected (i and j), but for the product_categories only the last key is written. I know it's probably getting overwritten, but I can't understand why?



      top_10_recs = {}

      for i, j, k in itertools.product(customers, categories, product_categories):
      if i not in top_10_recs:
      top_10_recs[i] = {}
      if j not in top_10_recs:
      top_10_recs[i][j] = {}
      if k not in top_10_recs:
      top_10_recs[i][j][k] = {}
      try:
      top_10_recs[i][j][k] = trained_dataframe.loc[(i, j), k].nlargest(10).to_dict()
      except:
      pass


      If I do a print with this:



      for i, j, k in itertools.product(customers, categories, product_categories):
      try:
      print("{}".format(k))
      print(trained_dataframe.loc[(i, j), k].nlargest(10))


      I get the expected result, looping through all product_categories.










      share|improve this question













      I want to create a nested dict through a for loop. All the customers and categories keys are created as expected (i and j), but for the product_categories only the last key is written. I know it's probably getting overwritten, but I can't understand why?



      top_10_recs = {}

      for i, j, k in itertools.product(customers, categories, product_categories):
      if i not in top_10_recs:
      top_10_recs[i] = {}
      if j not in top_10_recs:
      top_10_recs[i][j] = {}
      if k not in top_10_recs:
      top_10_recs[i][j][k] = {}
      try:
      top_10_recs[i][j][k] = trained_dataframe.loc[(i, j), k].nlargest(10).to_dict()
      except:
      pass


      If I do a print with this:



      for i, j, k in itertools.product(customers, categories, product_categories):
      try:
      print("{}".format(k))
      print(trained_dataframe.loc[(i, j), k].nlargest(10))


      I get the expected result, looping through all product_categories.







      python






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked yesterday









      joddm

      1661110




      1661110
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          You need to be careful to only create new keys inside dictionaries if they do not already exist in those dictionaries. The snippet below should work.



          if i not in top_10_recs:
          top_10_recs[i] = {}
          if j not in top_10_recs[i]:
          top_10_recs[i][j] = {}
          if k not in top_10_recs[i][j]:
          top_10_recs[i][j][k] = {}


          Consider also using except KeyError instead of just except, which catches all possible errors (you do not want that).






          share|improve this answer










          New contributor




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


















          • Thanks! Now all the customers and product_categories (i and k) are written to the dict, but only one category (j)
            – joddm
            yesterday






          • 1




            I may have misunderstood then. Let's consider the first customer object. You create a first entry which contains a new dictionary. Then, in this new dictionary, do you want as many keys (and thereby new inner dictionaries) as you have objects in categories, or just one ?
            – Patol75
            yesterday












          • Yes, I want to have all the objects in categories as keys in the dict
            – joddm
            yesterday










          • @joddm I have just understood the itertools.product() behaviour. Sorry for that, not used to using it, but it is indeed really powerful. Please see the updated code above. The key is indeed to only create inner directories if they have not already been created, and there is no need for indentation as I had previously suggested, because of the behavior of itertools.product(). I hope it works now.
            – Patol75
            yesterday










          • It works :) Thanks for helping me out!
            – joddm
            yesterday











          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%2f53371969%2fnested-dict-key-overwritten%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          1
          down vote



          accepted










          You need to be careful to only create new keys inside dictionaries if they do not already exist in those dictionaries. The snippet below should work.



          if i not in top_10_recs:
          top_10_recs[i] = {}
          if j not in top_10_recs[i]:
          top_10_recs[i][j] = {}
          if k not in top_10_recs[i][j]:
          top_10_recs[i][j][k] = {}


          Consider also using except KeyError instead of just except, which catches all possible errors (you do not want that).






          share|improve this answer










          New contributor




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


















          • Thanks! Now all the customers and product_categories (i and k) are written to the dict, but only one category (j)
            – joddm
            yesterday






          • 1




            I may have misunderstood then. Let's consider the first customer object. You create a first entry which contains a new dictionary. Then, in this new dictionary, do you want as many keys (and thereby new inner dictionaries) as you have objects in categories, or just one ?
            – Patol75
            yesterday












          • Yes, I want to have all the objects in categories as keys in the dict
            – joddm
            yesterday










          • @joddm I have just understood the itertools.product() behaviour. Sorry for that, not used to using it, but it is indeed really powerful. Please see the updated code above. The key is indeed to only create inner directories if they have not already been created, and there is no need for indentation as I had previously suggested, because of the behavior of itertools.product(). I hope it works now.
            – Patol75
            yesterday










          • It works :) Thanks for helping me out!
            – joddm
            yesterday















          up vote
          1
          down vote



          accepted










          You need to be careful to only create new keys inside dictionaries if they do not already exist in those dictionaries. The snippet below should work.



          if i not in top_10_recs:
          top_10_recs[i] = {}
          if j not in top_10_recs[i]:
          top_10_recs[i][j] = {}
          if k not in top_10_recs[i][j]:
          top_10_recs[i][j][k] = {}


          Consider also using except KeyError instead of just except, which catches all possible errors (you do not want that).






          share|improve this answer










          New contributor




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


















          • Thanks! Now all the customers and product_categories (i and k) are written to the dict, but only one category (j)
            – joddm
            yesterday






          • 1




            I may have misunderstood then. Let's consider the first customer object. You create a first entry which contains a new dictionary. Then, in this new dictionary, do you want as many keys (and thereby new inner dictionaries) as you have objects in categories, or just one ?
            – Patol75
            yesterday












          • Yes, I want to have all the objects in categories as keys in the dict
            – joddm
            yesterday










          • @joddm I have just understood the itertools.product() behaviour. Sorry for that, not used to using it, but it is indeed really powerful. Please see the updated code above. The key is indeed to only create inner directories if they have not already been created, and there is no need for indentation as I had previously suggested, because of the behavior of itertools.product(). I hope it works now.
            – Patol75
            yesterday










          • It works :) Thanks for helping me out!
            – joddm
            yesterday













          up vote
          1
          down vote



          accepted







          up vote
          1
          down vote



          accepted






          You need to be careful to only create new keys inside dictionaries if they do not already exist in those dictionaries. The snippet below should work.



          if i not in top_10_recs:
          top_10_recs[i] = {}
          if j not in top_10_recs[i]:
          top_10_recs[i][j] = {}
          if k not in top_10_recs[i][j]:
          top_10_recs[i][j][k] = {}


          Consider also using except KeyError instead of just except, which catches all possible errors (you do not want that).






          share|improve this answer










          New contributor




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









          You need to be careful to only create new keys inside dictionaries if they do not already exist in those dictionaries. The snippet below should work.



          if i not in top_10_recs:
          top_10_recs[i] = {}
          if j not in top_10_recs[i]:
          top_10_recs[i][j] = {}
          if k not in top_10_recs[i][j]:
          top_10_recs[i][j][k] = {}


          Consider also using except KeyError instead of just except, which catches all possible errors (you do not want that).







          share|improve this answer










          New contributor




          Patol75 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








          edited yesterday





















          New contributor




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









          answered yesterday









          Patol75

          5216




          5216




          New contributor




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





          New contributor





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






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












          • Thanks! Now all the customers and product_categories (i and k) are written to the dict, but only one category (j)
            – joddm
            yesterday






          • 1




            I may have misunderstood then. Let's consider the first customer object. You create a first entry which contains a new dictionary. Then, in this new dictionary, do you want as many keys (and thereby new inner dictionaries) as you have objects in categories, or just one ?
            – Patol75
            yesterday












          • Yes, I want to have all the objects in categories as keys in the dict
            – joddm
            yesterday










          • @joddm I have just understood the itertools.product() behaviour. Sorry for that, not used to using it, but it is indeed really powerful. Please see the updated code above. The key is indeed to only create inner directories if they have not already been created, and there is no need for indentation as I had previously suggested, because of the behavior of itertools.product(). I hope it works now.
            – Patol75
            yesterday










          • It works :) Thanks for helping me out!
            – joddm
            yesterday


















          • Thanks! Now all the customers and product_categories (i and k) are written to the dict, but only one category (j)
            – joddm
            yesterday






          • 1




            I may have misunderstood then. Let's consider the first customer object. You create a first entry which contains a new dictionary. Then, in this new dictionary, do you want as many keys (and thereby new inner dictionaries) as you have objects in categories, or just one ?
            – Patol75
            yesterday












          • Yes, I want to have all the objects in categories as keys in the dict
            – joddm
            yesterday










          • @joddm I have just understood the itertools.product() behaviour. Sorry for that, not used to using it, but it is indeed really powerful. Please see the updated code above. The key is indeed to only create inner directories if they have not already been created, and there is no need for indentation as I had previously suggested, because of the behavior of itertools.product(). I hope it works now.
            – Patol75
            yesterday










          • It works :) Thanks for helping me out!
            – joddm
            yesterday
















          Thanks! Now all the customers and product_categories (i and k) are written to the dict, but only one category (j)
          – joddm
          yesterday




          Thanks! Now all the customers and product_categories (i and k) are written to the dict, but only one category (j)
          – joddm
          yesterday




          1




          1




          I may have misunderstood then. Let's consider the first customer object. You create a first entry which contains a new dictionary. Then, in this new dictionary, do you want as many keys (and thereby new inner dictionaries) as you have objects in categories, or just one ?
          – Patol75
          yesterday






          I may have misunderstood then. Let's consider the first customer object. You create a first entry which contains a new dictionary. Then, in this new dictionary, do you want as many keys (and thereby new inner dictionaries) as you have objects in categories, or just one ?
          – Patol75
          yesterday














          Yes, I want to have all the objects in categories as keys in the dict
          – joddm
          yesterday




          Yes, I want to have all the objects in categories as keys in the dict
          – joddm
          yesterday












          @joddm I have just understood the itertools.product() behaviour. Sorry for that, not used to using it, but it is indeed really powerful. Please see the updated code above. The key is indeed to only create inner directories if they have not already been created, and there is no need for indentation as I had previously suggested, because of the behavior of itertools.product(). I hope it works now.
          – Patol75
          yesterday




          @joddm I have just understood the itertools.product() behaviour. Sorry for that, not used to using it, but it is indeed really powerful. Please see the updated code above. The key is indeed to only create inner directories if they have not already been created, and there is no need for indentation as I had previously suggested, because of the behavior of itertools.product(). I hope it works now.
          – Patol75
          yesterday












          It works :) Thanks for helping me out!
          – joddm
          yesterday




          It works :) Thanks for helping me out!
          – joddm
          yesterday


















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53371969%2fnested-dict-key-overwritten%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Volksrepublik China

          Saint-Aignan (Tarn-et-Garonne)

          How to test boost logger output in unit testing?