Is there any way to map a list of elements to a dictionary without a loop?












1















I want to create 2 dictionaries by mapping 2 lists from a central dictionary, without using a loop.



Input dictionary:



edict_all = { 1:[[23,20]], 2:[[45,45]], 3:[[56,43]], 4:[[66,23]], 5:[[24,23]], 9:[[57,78]], 8:[[67,76]], 51:[[242,223]]}


And I have 2 lists:



list_a = [1,4,8,9,51]
list_b = [1,2,3,5,9]


Currently, I am using 2 for loops:



edict_a, edict_b = dict(), dict()
for i in list_a:
edict_a[i] = edict_all[i]

for i in list_b:
edict_b[i] = edict_all[i]


And the output is:



edict_a = {1: [[23, 20]], 4: [[66, 23]], 8: [[67, 76]], 9: [[57, 78]], 51: [[242, 223]]}

edict_b = {1: [[23, 20]], 2: [[45, 45]], 3: [[56, 43]], 5: [[24, 23]], 9: [[57, 78]]}









share|improve this question

























  • Not aware of a way to get what you are looking for without python looping through the values. There are lots of ways to get what you want with list or dict comprehensions as well as built-in functions that may not have the appearance of the looping approach you are using (and may be more efficient) but in fact are all looping through the data.

    – benvc
    Nov 14 '18 at 16:24


















1















I want to create 2 dictionaries by mapping 2 lists from a central dictionary, without using a loop.



Input dictionary:



edict_all = { 1:[[23,20]], 2:[[45,45]], 3:[[56,43]], 4:[[66,23]], 5:[[24,23]], 9:[[57,78]], 8:[[67,76]], 51:[[242,223]]}


And I have 2 lists:



list_a = [1,4,8,9,51]
list_b = [1,2,3,5,9]


Currently, I am using 2 for loops:



edict_a, edict_b = dict(), dict()
for i in list_a:
edict_a[i] = edict_all[i]

for i in list_b:
edict_b[i] = edict_all[i]


And the output is:



edict_a = {1: [[23, 20]], 4: [[66, 23]], 8: [[67, 76]], 9: [[57, 78]], 51: [[242, 223]]}

edict_b = {1: [[23, 20]], 2: [[45, 45]], 3: [[56, 43]], 5: [[24, 23]], 9: [[57, 78]]}









share|improve this question

























  • Not aware of a way to get what you are looking for without python looping through the values. There are lots of ways to get what you want with list or dict comprehensions as well as built-in functions that may not have the appearance of the looping approach you are using (and may be more efficient) but in fact are all looping through the data.

    – benvc
    Nov 14 '18 at 16:24
















1












1








1








I want to create 2 dictionaries by mapping 2 lists from a central dictionary, without using a loop.



Input dictionary:



edict_all = { 1:[[23,20]], 2:[[45,45]], 3:[[56,43]], 4:[[66,23]], 5:[[24,23]], 9:[[57,78]], 8:[[67,76]], 51:[[242,223]]}


And I have 2 lists:



list_a = [1,4,8,9,51]
list_b = [1,2,3,5,9]


Currently, I am using 2 for loops:



edict_a, edict_b = dict(), dict()
for i in list_a:
edict_a[i] = edict_all[i]

for i in list_b:
edict_b[i] = edict_all[i]


And the output is:



edict_a = {1: [[23, 20]], 4: [[66, 23]], 8: [[67, 76]], 9: [[57, 78]], 51: [[242, 223]]}

edict_b = {1: [[23, 20]], 2: [[45, 45]], 3: [[56, 43]], 5: [[24, 23]], 9: [[57, 78]]}









share|improve this question
















I want to create 2 dictionaries by mapping 2 lists from a central dictionary, without using a loop.



Input dictionary:



edict_all = { 1:[[23,20]], 2:[[45,45]], 3:[[56,43]], 4:[[66,23]], 5:[[24,23]], 9:[[57,78]], 8:[[67,76]], 51:[[242,223]]}


And I have 2 lists:



list_a = [1,4,8,9,51]
list_b = [1,2,3,5,9]


Currently, I am using 2 for loops:



edict_a, edict_b = dict(), dict()
for i in list_a:
edict_a[i] = edict_all[i]

for i in list_b:
edict_b[i] = edict_all[i]


And the output is:



edict_a = {1: [[23, 20]], 4: [[66, 23]], 8: [[67, 76]], 9: [[57, 78]], 51: [[242, 223]]}

edict_b = {1: [[23, 20]], 2: [[45, 45]], 3: [[56, 43]], 5: [[24, 23]], 9: [[57, 78]]}






python python-3.x list dictionary mapping






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 17:14









jpp

100k2162111




100k2162111










asked Nov 14 '18 at 15:15









Gurpreet.SGurpreet.S

546




546













  • Not aware of a way to get what you are looking for without python looping through the values. There are lots of ways to get what you want with list or dict comprehensions as well as built-in functions that may not have the appearance of the looping approach you are using (and may be more efficient) but in fact are all looping through the data.

    – benvc
    Nov 14 '18 at 16:24





















  • Not aware of a way to get what you are looking for without python looping through the values. There are lots of ways to get what you want with list or dict comprehensions as well as built-in functions that may not have the appearance of the looping approach you are using (and may be more efficient) but in fact are all looping through the data.

    – benvc
    Nov 14 '18 at 16:24



















Not aware of a way to get what you are looking for without python looping through the values. There are lots of ways to get what you want with list or dict comprehensions as well as built-in functions that may not have the appearance of the looping approach you are using (and may be more efficient) but in fact are all looping through the data.

– benvc
Nov 14 '18 at 16:24







Not aware of a way to get what you are looking for without python looping through the values. There are lots of ways to get what you want with list or dict comprehensions as well as built-in functions that may not have the appearance of the looping approach you are using (and may be more efficient) but in fact are all looping through the data.

– benvc
Nov 14 '18 at 16:24














2 Answers
2






active

oldest

votes


















1














Loops at some level are unavoidable here, but you can hide them with zip and operator.itemgetter:



from operator import itemgetter

edict_a = dict(zip(list_a, itemgetter(*list_a)(edict_all)))
edict_b = dict(zip(list_b, itemgetter(*list_b)(edict_all)))





share|improve this answer































    0














    You can use Python generator expression to filter data and then pass it back to dict() constructor:



    edict_a = dict((key, value) for key, value in edict_all.items() if key in list_a)
    edict_b = dict((key, value) for key, value in edict_all.items() if key in list_b)


    And yes, loops in any form are unavoidable for this problem. At least you can write it in one line.






    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',
      autoActivateHeartbeat: false,
      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%2f53303371%2fis-there-any-way-to-map-a-list-of-elements-to-a-dictionary-without-a-loop%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      Loops at some level are unavoidable here, but you can hide them with zip and operator.itemgetter:



      from operator import itemgetter

      edict_a = dict(zip(list_a, itemgetter(*list_a)(edict_all)))
      edict_b = dict(zip(list_b, itemgetter(*list_b)(edict_all)))





      share|improve this answer




























        1














        Loops at some level are unavoidable here, but you can hide them with zip and operator.itemgetter:



        from operator import itemgetter

        edict_a = dict(zip(list_a, itemgetter(*list_a)(edict_all)))
        edict_b = dict(zip(list_b, itemgetter(*list_b)(edict_all)))





        share|improve this answer


























          1












          1








          1







          Loops at some level are unavoidable here, but you can hide them with zip and operator.itemgetter:



          from operator import itemgetter

          edict_a = dict(zip(list_a, itemgetter(*list_a)(edict_all)))
          edict_b = dict(zip(list_b, itemgetter(*list_b)(edict_all)))





          share|improve this answer













          Loops at some level are unavoidable here, but you can hide them with zip and operator.itemgetter:



          from operator import itemgetter

          edict_a = dict(zip(list_a, itemgetter(*list_a)(edict_all)))
          edict_b = dict(zip(list_b, itemgetter(*list_b)(edict_all)))






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 14 '18 at 17:13









          jppjpp

          100k2162111




          100k2162111

























              0














              You can use Python generator expression to filter data and then pass it back to dict() constructor:



              edict_a = dict((key, value) for key, value in edict_all.items() if key in list_a)
              edict_b = dict((key, value) for key, value in edict_all.items() if key in list_b)


              And yes, loops in any form are unavoidable for this problem. At least you can write it in one line.






              share|improve this answer




























                0














                You can use Python generator expression to filter data and then pass it back to dict() constructor:



                edict_a = dict((key, value) for key, value in edict_all.items() if key in list_a)
                edict_b = dict((key, value) for key, value in edict_all.items() if key in list_b)


                And yes, loops in any form are unavoidable for this problem. At least you can write it in one line.






                share|improve this answer


























                  0












                  0








                  0







                  You can use Python generator expression to filter data and then pass it back to dict() constructor:



                  edict_a = dict((key, value) for key, value in edict_all.items() if key in list_a)
                  edict_b = dict((key, value) for key, value in edict_all.items() if key in list_b)


                  And yes, loops in any form are unavoidable for this problem. At least you can write it in one line.






                  share|improve this answer













                  You can use Python generator expression to filter data and then pass it back to dict() constructor:



                  edict_a = dict((key, value) for key, value in edict_all.items() if key in list_a)
                  edict_b = dict((key, value) for key, value in edict_all.items() if key in list_b)


                  And yes, loops in any form are unavoidable for this problem. At least you can write it in one line.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 14 '18 at 18:10









                  Andrey SemakinAndrey Semakin

                  12017




                  12017






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53303371%2fis-there-any-way-to-map-a-list-of-elements-to-a-dictionary-without-a-loop%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

                      Bressuire

                      Vorschmack

                      Quarantine