Which is the fastest way of creating a copy of dictionary without specific (sub)keys? [on hold]












-2















I need to know the fastest way of getting a copy of dictionary without specific key.



I have a dict like this:



users = {
1: {'state': 'apple', 'menu_id': 123, 'args':['some data']},
2: {'state': 'something', 'menu_id': 321},
3: {'state': 'banana', 'menu_id': 666, 'args':['trash', 'temporary']}
}


I need to get a copy of users without 'args' key
I've solved this problem in three ways and there are my 3 solutions:



s = users.copy()
for k,v in obj.items(): v.pop('args', None)

s1 = {k: {'state': v['state'], 'menu_id':v['menu_id']} for k,v in users.items()}

s2 = {k: {p: q for p,q in v.items() if p != 'args'} for k, v in users.items()}


Which solution is faster or are there any other faster ways to get the expected result?










share|improve this question















put on hold as primarily opinion-based by Azat Ibrakov, Larry Shatzer, TylerH, Bob Dalgleish, Eray Balkanli Jan 18 at 19:06


Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise. If this question can be reworded to fit the rules in the help center, please edit the question.




















    -2















    I need to know the fastest way of getting a copy of dictionary without specific key.



    I have a dict like this:



    users = {
    1: {'state': 'apple', 'menu_id': 123, 'args':['some data']},
    2: {'state': 'something', 'menu_id': 321},
    3: {'state': 'banana', 'menu_id': 666, 'args':['trash', 'temporary']}
    }


    I need to get a copy of users without 'args' key
    I've solved this problem in three ways and there are my 3 solutions:



    s = users.copy()
    for k,v in obj.items(): v.pop('args', None)

    s1 = {k: {'state': v['state'], 'menu_id':v['menu_id']} for k,v in users.items()}

    s2 = {k: {p: q for p,q in v.items() if p != 'args'} for k, v in users.items()}


    Which solution is faster or are there any other faster ways to get the expected result?










    share|improve this question















    put on hold as primarily opinion-based by Azat Ibrakov, Larry Shatzer, TylerH, Bob Dalgleish, Eray Balkanli Jan 18 at 19:06


    Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise. If this question can be reworded to fit the rules in the help center, please edit the question.


















      -2












      -2








      -2








      I need to know the fastest way of getting a copy of dictionary without specific key.



      I have a dict like this:



      users = {
      1: {'state': 'apple', 'menu_id': 123, 'args':['some data']},
      2: {'state': 'something', 'menu_id': 321},
      3: {'state': 'banana', 'menu_id': 666, 'args':['trash', 'temporary']}
      }


      I need to get a copy of users without 'args' key
      I've solved this problem in three ways and there are my 3 solutions:



      s = users.copy()
      for k,v in obj.items(): v.pop('args', None)

      s1 = {k: {'state': v['state'], 'menu_id':v['menu_id']} for k,v in users.items()}

      s2 = {k: {p: q for p,q in v.items() if p != 'args'} for k, v in users.items()}


      Which solution is faster or are there any other faster ways to get the expected result?










      share|improve this question
















      I need to know the fastest way of getting a copy of dictionary without specific key.



      I have a dict like this:



      users = {
      1: {'state': 'apple', 'menu_id': 123, 'args':['some data']},
      2: {'state': 'something', 'menu_id': 321},
      3: {'state': 'banana', 'menu_id': 666, 'args':['trash', 'temporary']}
      }


      I need to get a copy of users without 'args' key
      I've solved this problem in three ways and there are my 3 solutions:



      s = users.copy()
      for k,v in obj.items(): v.pop('args', None)

      s1 = {k: {'state': v['state'], 'menu_id':v['menu_id']} for k,v in users.items()}

      s2 = {k: {p: q for p,q in v.items() if p != 'args'} for k, v in users.items()}


      Which solution is faster or are there any other faster ways to get the expected result?







      python python-3.x performance dictionary key






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 18 at 14:55









      Georgy

      2,04331026




      2,04331026










      asked Jan 18 at 12:51









      GooDeeJaYGooDeeJaY

      7216




      7216




      put on hold as primarily opinion-based by Azat Ibrakov, Larry Shatzer, TylerH, Bob Dalgleish, Eray Balkanli Jan 18 at 19:06


      Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise. If this question can be reworded to fit the rules in the help center, please edit the question.






      put on hold as primarily opinion-based by Azat Ibrakov, Larry Shatzer, TylerH, Bob Dalgleish, Eray Balkanli Jan 18 at 19:06


      Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise. If this question can be reworded to fit the rules in the help center, please edit the question.


























          2 Answers
          2






          active

          oldest

          votes


















          1















          • First approach is not even viable — it's destructive for original data. You're making copy, not deepcopy, which means that for k,v in obj.items(): v.pop('args', None) will mutate sub-dicts in original structure.

          • 2nd approach is (in my opinion) semantically wrong — you're not "creating without specific key", you're creating new "based on specific key". This is different at all, and this is not a technical question, this is question for business logic.

          • 3rd one is the only one that matches for what you're trying to achieve, and this is pretty idiomatic way to do this in python.


          Even if performance should not bother you until you'll face performance problems, but anyway, considering 3rd one as base:




          • destructive approach is fastest

          • 1st approach with deepcopy is magnitude slower than 3rd one

          • 2nd is ~2 times faster than 3rd






          share|improve this answer































            1














            First of all, I wouldn't use s2 in anywhere. Because it's hard to read. Python code should be readable.



            And when it comes to s1 and s, there are kinda different. copy function uses the same object reference with your original object. So after you applied solution s, if you make changes in dictionaries in users, you'll see your changes in s. But if you use s1, because of the new objects are created, the changes will not occur in s1.






            share|improve this answer






























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              1















              • First approach is not even viable — it's destructive for original data. You're making copy, not deepcopy, which means that for k,v in obj.items(): v.pop('args', None) will mutate sub-dicts in original structure.

              • 2nd approach is (in my opinion) semantically wrong — you're not "creating without specific key", you're creating new "based on specific key". This is different at all, and this is not a technical question, this is question for business logic.

              • 3rd one is the only one that matches for what you're trying to achieve, and this is pretty idiomatic way to do this in python.


              Even if performance should not bother you until you'll face performance problems, but anyway, considering 3rd one as base:




              • destructive approach is fastest

              • 1st approach with deepcopy is magnitude slower than 3rd one

              • 2nd is ~2 times faster than 3rd






              share|improve this answer




























                1















                • First approach is not even viable — it's destructive for original data. You're making copy, not deepcopy, which means that for k,v in obj.items(): v.pop('args', None) will mutate sub-dicts in original structure.

                • 2nd approach is (in my opinion) semantically wrong — you're not "creating without specific key", you're creating new "based on specific key". This is different at all, and this is not a technical question, this is question for business logic.

                • 3rd one is the only one that matches for what you're trying to achieve, and this is pretty idiomatic way to do this in python.


                Even if performance should not bother you until you'll face performance problems, but anyway, considering 3rd one as base:




                • destructive approach is fastest

                • 1st approach with deepcopy is magnitude slower than 3rd one

                • 2nd is ~2 times faster than 3rd






                share|improve this answer


























                  1












                  1








                  1








                  • First approach is not even viable — it's destructive for original data. You're making copy, not deepcopy, which means that for k,v in obj.items(): v.pop('args', None) will mutate sub-dicts in original structure.

                  • 2nd approach is (in my opinion) semantically wrong — you're not "creating without specific key", you're creating new "based on specific key". This is different at all, and this is not a technical question, this is question for business logic.

                  • 3rd one is the only one that matches for what you're trying to achieve, and this is pretty idiomatic way to do this in python.


                  Even if performance should not bother you until you'll face performance problems, but anyway, considering 3rd one as base:




                  • destructive approach is fastest

                  • 1st approach with deepcopy is magnitude slower than 3rd one

                  • 2nd is ~2 times faster than 3rd






                  share|improve this answer














                  • First approach is not even viable — it's destructive for original data. You're making copy, not deepcopy, which means that for k,v in obj.items(): v.pop('args', None) will mutate sub-dicts in original structure.

                  • 2nd approach is (in my opinion) semantically wrong — you're not "creating without specific key", you're creating new "based on specific key". This is different at all, and this is not a technical question, this is question for business logic.

                  • 3rd one is the only one that matches for what you're trying to achieve, and this is pretty idiomatic way to do this in python.


                  Even if performance should not bother you until you'll face performance problems, but anyway, considering 3rd one as base:




                  • destructive approach is fastest

                  • 1st approach with deepcopy is magnitude slower than 3rd one

                  • 2nd is ~2 times faster than 3rd







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jan 18 at 13:19









                  SlamSlam

                  4,13012033




                  4,13012033

























                      1














                      First of all, I wouldn't use s2 in anywhere. Because it's hard to read. Python code should be readable.



                      And when it comes to s1 and s, there are kinda different. copy function uses the same object reference with your original object. So after you applied solution s, if you make changes in dictionaries in users, you'll see your changes in s. But if you use s1, because of the new objects are created, the changes will not occur in s1.






                      share|improve this answer




























                        1














                        First of all, I wouldn't use s2 in anywhere. Because it's hard to read. Python code should be readable.



                        And when it comes to s1 and s, there are kinda different. copy function uses the same object reference with your original object. So after you applied solution s, if you make changes in dictionaries in users, you'll see your changes in s. But if you use s1, because of the new objects are created, the changes will not occur in s1.






                        share|improve this answer


























                          1












                          1








                          1







                          First of all, I wouldn't use s2 in anywhere. Because it's hard to read. Python code should be readable.



                          And when it comes to s1 and s, there are kinda different. copy function uses the same object reference with your original object. So after you applied solution s, if you make changes in dictionaries in users, you'll see your changes in s. But if you use s1, because of the new objects are created, the changes will not occur in s1.






                          share|improve this answer













                          First of all, I wouldn't use s2 in anywhere. Because it's hard to read. Python code should be readable.



                          And when it comes to s1 and s, there are kinda different. copy function uses the same object reference with your original object. So after you applied solution s, if you make changes in dictionaries in users, you'll see your changes in s. But if you use s1, because of the new objects are created, the changes will not occur in s1.







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Jan 18 at 13:19









                          nejdetckenobinejdetckenobi

                          110110




                          110110















                              Popular posts from this blog

                              Liquibase includeAll doesn't find base path

                              How to use setInterval in EJS file?

                              Petrus Granier-Deferre