How to divide one list into 2 using a For loop?

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











up vote
2
down vote

favorite












Say I have a list called A and I want to divide it into two separate list B and C by looping through A and picking out one element to add to B and one to add to A throughout the length of A.










share|improve this question







New contributor




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















  • 1




    Welcome to Mma.SE. Start by taking the tour now and learning about asking and what's on-topic. Always edit if improvable, show due diligence, give brief context, include minimal working example of code and data in formatted form. By doing all this you help us to help you and likely you will inspire great answers. You need to provide us with: the data, the criteria to Select elements, which Cases define how to Split the list, and most importantly: the code of what you have tried so far.
    – rhermans
    15 hours ago







  • 4




    A hint: the best solution will not use a For loop, but probably something in the lines of Select, Cases or Split. Your question may be put on-hold because its unanswerable for lack of details. We can't help unless you edit your question to improve it and make it specific, with all the details one would need to reproduce your problem exactly. Please don't be discouraged by that cleaning-up policy. Your questions are and will be most welcomed. Learn about good questions here.
    – rhermans
    14 hours ago







  • 1




    A For loop is a very awkward and inefficient way to accomplish what you seek. By what criterion do you place elements into $B$ or $C$? Simple alternates?
    – David G. Stork
    14 hours ago










  • Yes, Simple alternatives or even random separation
    – Bryan
    14 hours ago














up vote
2
down vote

favorite












Say I have a list called A and I want to divide it into two separate list B and C by looping through A and picking out one element to add to B and one to add to A throughout the length of A.










share|improve this question







New contributor




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















  • 1




    Welcome to Mma.SE. Start by taking the tour now and learning about asking and what's on-topic. Always edit if improvable, show due diligence, give brief context, include minimal working example of code and data in formatted form. By doing all this you help us to help you and likely you will inspire great answers. You need to provide us with: the data, the criteria to Select elements, which Cases define how to Split the list, and most importantly: the code of what you have tried so far.
    – rhermans
    15 hours ago







  • 4




    A hint: the best solution will not use a For loop, but probably something in the lines of Select, Cases or Split. Your question may be put on-hold because its unanswerable for lack of details. We can't help unless you edit your question to improve it and make it specific, with all the details one would need to reproduce your problem exactly. Please don't be discouraged by that cleaning-up policy. Your questions are and will be most welcomed. Learn about good questions here.
    – rhermans
    14 hours ago







  • 1




    A For loop is a very awkward and inefficient way to accomplish what you seek. By what criterion do you place elements into $B$ or $C$? Simple alternates?
    – David G. Stork
    14 hours ago










  • Yes, Simple alternatives or even random separation
    – Bryan
    14 hours ago












up vote
2
down vote

favorite









up vote
2
down vote

favorite











Say I have a list called A and I want to divide it into two separate list B and C by looping through A and picking out one element to add to B and one to add to A throughout the length of A.










share|improve this question







New contributor




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











Say I have a list called A and I want to divide it into two separate list B and C by looping through A and picking out one element to add to B and one to add to A throughout the length of A.







list-manipulation






share|improve this question







New contributor




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











share|improve this question







New contributor




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









share|improve this question




share|improve this question






New contributor




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









asked 15 hours ago









Bryan

191




191




New contributor




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





New contributor





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






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







  • 1




    Welcome to Mma.SE. Start by taking the tour now and learning about asking and what's on-topic. Always edit if improvable, show due diligence, give brief context, include minimal working example of code and data in formatted form. By doing all this you help us to help you and likely you will inspire great answers. You need to provide us with: the data, the criteria to Select elements, which Cases define how to Split the list, and most importantly: the code of what you have tried so far.
    – rhermans
    15 hours ago







  • 4




    A hint: the best solution will not use a For loop, but probably something in the lines of Select, Cases or Split. Your question may be put on-hold because its unanswerable for lack of details. We can't help unless you edit your question to improve it and make it specific, with all the details one would need to reproduce your problem exactly. Please don't be discouraged by that cleaning-up policy. Your questions are and will be most welcomed. Learn about good questions here.
    – rhermans
    14 hours ago







  • 1




    A For loop is a very awkward and inefficient way to accomplish what you seek. By what criterion do you place elements into $B$ or $C$? Simple alternates?
    – David G. Stork
    14 hours ago










  • Yes, Simple alternatives or even random separation
    – Bryan
    14 hours ago












  • 1




    Welcome to Mma.SE. Start by taking the tour now and learning about asking and what's on-topic. Always edit if improvable, show due diligence, give brief context, include minimal working example of code and data in formatted form. By doing all this you help us to help you and likely you will inspire great answers. You need to provide us with: the data, the criteria to Select elements, which Cases define how to Split the list, and most importantly: the code of what you have tried so far.
    – rhermans
    15 hours ago







  • 4




    A hint: the best solution will not use a For loop, but probably something in the lines of Select, Cases or Split. Your question may be put on-hold because its unanswerable for lack of details. We can't help unless you edit your question to improve it and make it specific, with all the details one would need to reproduce your problem exactly. Please don't be discouraged by that cleaning-up policy. Your questions are and will be most welcomed. Learn about good questions here.
    – rhermans
    14 hours ago







  • 1




    A For loop is a very awkward and inefficient way to accomplish what you seek. By what criterion do you place elements into $B$ or $C$? Simple alternates?
    – David G. Stork
    14 hours ago










  • Yes, Simple alternatives or even random separation
    – Bryan
    14 hours ago







1




1




Welcome to Mma.SE. Start by taking the tour now and learning about asking and what's on-topic. Always edit if improvable, show due diligence, give brief context, include minimal working example of code and data in formatted form. By doing all this you help us to help you and likely you will inspire great answers. You need to provide us with: the data, the criteria to Select elements, which Cases define how to Split the list, and most importantly: the code of what you have tried so far.
– rhermans
15 hours ago





Welcome to Mma.SE. Start by taking the tour now and learning about asking and what's on-topic. Always edit if improvable, show due diligence, give brief context, include minimal working example of code and data in formatted form. By doing all this you help us to help you and likely you will inspire great answers. You need to provide us with: the data, the criteria to Select elements, which Cases define how to Split the list, and most importantly: the code of what you have tried so far.
– rhermans
15 hours ago





4




4




A hint: the best solution will not use a For loop, but probably something in the lines of Select, Cases or Split. Your question may be put on-hold because its unanswerable for lack of details. We can't help unless you edit your question to improve it and make it specific, with all the details one would need to reproduce your problem exactly. Please don't be discouraged by that cleaning-up policy. Your questions are and will be most welcomed. Learn about good questions here.
– rhermans
14 hours ago





A hint: the best solution will not use a For loop, but probably something in the lines of Select, Cases or Split. Your question may be put on-hold because its unanswerable for lack of details. We can't help unless you edit your question to improve it and make it specific, with all the details one would need to reproduce your problem exactly. Please don't be discouraged by that cleaning-up policy. Your questions are and will be most welcomed. Learn about good questions here.
– rhermans
14 hours ago





1




1




A For loop is a very awkward and inefficient way to accomplish what you seek. By what criterion do you place elements into $B$ or $C$? Simple alternates?
– David G. Stork
14 hours ago




A For loop is a very awkward and inefficient way to accomplish what you seek. By what criterion do you place elements into $B$ or $C$? Simple alternates?
– David G. Stork
14 hours ago












Yes, Simple alternatives or even random separation
– Bryan
14 hours ago




Yes, Simple alternatives or even random separation
– Bryan
14 hours ago










3 Answers
3






active

oldest

votes

















up vote
6
down vote













Don't use For. Really. Never.



There are much more elegant ways, for example



a = Range[20];
b, c = Transpose[Partition[a, 2]]



1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 2, 4, 6, 8, 10, 12, 14, 16, 18,
20




or (should be the fastest)



b = a[[1;;;;2]]
c = a[[2;;;;2]]



1, 3, 5, 7, 9, 11, 13, 15, 17, 19



2, 4, 6, 8, 10, 12, 14, 16, 18, 20




or



b, c = GatherBy[a, OddQ]



1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 2, 4, 6, 8, 10, 12, 14, 16, 18,
20




or



b = Select[a, OddQ]
c = Select[a, EvenQ]



1, 3, 5, 7, 9, 11, 13, 15, 17, 19



2, 4, 6, 8, 10, 12, 14, 16, 18, 20




In the last two examples, you may replace OddQ and EvenQ by any other function that returns True or False in order to realize other decision rules.






share|improve this answer






















  • Only the middle approach is robust. +1 for it.
    – Bob Hanlon
    9 hours ago

















up vote
4
down vote













As it turns out, there is a special function for taking every other element called DownSample:



list = Range[20];
a = Downsample[list, 2]
b = Downsample[list, 2, 2]


a has all the odd values, b has all the evens.






share|improve this answer



























    up vote
    2
    down vote













    list = Range@20; 


    here are alternatives



    TakeDrop[list, 1, -1, 2] 



    1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 2, 4, 6, 8, 10, 12, 14, 16, 18,
    20




    and here is random separation



    d = RandomSample[s, Length@list/2], Complement[list, d] 



    4, 12, 5, 19, 3, 11, 17, 16, 6, 10, 1, 2, 7, 8, 9, 13, 14, 15, 18,
    20







    share|improve this answer




















      Your Answer





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

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "387"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      convertImagesToLinks: false,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      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
      );



      );






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









       

      draft saved


      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f185103%2fhow-to-divide-one-list-into-2-using-a-for-loop%23new-answer', 'question_page');

      );

      Post as a guest






























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      6
      down vote













      Don't use For. Really. Never.



      There are much more elegant ways, for example



      a = Range[20];
      b, c = Transpose[Partition[a, 2]]



      1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 2, 4, 6, 8, 10, 12, 14, 16, 18,
      20




      or (should be the fastest)



      b = a[[1;;;;2]]
      c = a[[2;;;;2]]



      1, 3, 5, 7, 9, 11, 13, 15, 17, 19



      2, 4, 6, 8, 10, 12, 14, 16, 18, 20




      or



      b, c = GatherBy[a, OddQ]



      1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 2, 4, 6, 8, 10, 12, 14, 16, 18,
      20




      or



      b = Select[a, OddQ]
      c = Select[a, EvenQ]



      1, 3, 5, 7, 9, 11, 13, 15, 17, 19



      2, 4, 6, 8, 10, 12, 14, 16, 18, 20




      In the last two examples, you may replace OddQ and EvenQ by any other function that returns True or False in order to realize other decision rules.






      share|improve this answer






















      • Only the middle approach is robust. +1 for it.
        – Bob Hanlon
        9 hours ago














      up vote
      6
      down vote













      Don't use For. Really. Never.



      There are much more elegant ways, for example



      a = Range[20];
      b, c = Transpose[Partition[a, 2]]



      1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 2, 4, 6, 8, 10, 12, 14, 16, 18,
      20




      or (should be the fastest)



      b = a[[1;;;;2]]
      c = a[[2;;;;2]]



      1, 3, 5, 7, 9, 11, 13, 15, 17, 19



      2, 4, 6, 8, 10, 12, 14, 16, 18, 20




      or



      b, c = GatherBy[a, OddQ]



      1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 2, 4, 6, 8, 10, 12, 14, 16, 18,
      20




      or



      b = Select[a, OddQ]
      c = Select[a, EvenQ]



      1, 3, 5, 7, 9, 11, 13, 15, 17, 19



      2, 4, 6, 8, 10, 12, 14, 16, 18, 20




      In the last two examples, you may replace OddQ and EvenQ by any other function that returns True or False in order to realize other decision rules.






      share|improve this answer






















      • Only the middle approach is robust. +1 for it.
        – Bob Hanlon
        9 hours ago












      up vote
      6
      down vote










      up vote
      6
      down vote









      Don't use For. Really. Never.



      There are much more elegant ways, for example



      a = Range[20];
      b, c = Transpose[Partition[a, 2]]



      1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 2, 4, 6, 8, 10, 12, 14, 16, 18,
      20




      or (should be the fastest)



      b = a[[1;;;;2]]
      c = a[[2;;;;2]]



      1, 3, 5, 7, 9, 11, 13, 15, 17, 19



      2, 4, 6, 8, 10, 12, 14, 16, 18, 20




      or



      b, c = GatherBy[a, OddQ]



      1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 2, 4, 6, 8, 10, 12, 14, 16, 18,
      20




      or



      b = Select[a, OddQ]
      c = Select[a, EvenQ]



      1, 3, 5, 7, 9, 11, 13, 15, 17, 19



      2, 4, 6, 8, 10, 12, 14, 16, 18, 20




      In the last two examples, you may replace OddQ and EvenQ by any other function that returns True or False in order to realize other decision rules.






      share|improve this answer














      Don't use For. Really. Never.



      There are much more elegant ways, for example



      a = Range[20];
      b, c = Transpose[Partition[a, 2]]



      1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 2, 4, 6, 8, 10, 12, 14, 16, 18,
      20




      or (should be the fastest)



      b = a[[1;;;;2]]
      c = a[[2;;;;2]]



      1, 3, 5, 7, 9, 11, 13, 15, 17, 19



      2, 4, 6, 8, 10, 12, 14, 16, 18, 20




      or



      b, c = GatherBy[a, OddQ]



      1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 2, 4, 6, 8, 10, 12, 14, 16, 18,
      20




      or



      b = Select[a, OddQ]
      c = Select[a, EvenQ]



      1, 3, 5, 7, 9, 11, 13, 15, 17, 19



      2, 4, 6, 8, 10, 12, 14, 16, 18, 20




      In the last two examples, you may replace OddQ and EvenQ by any other function that returns True or False in order to realize other decision rules.







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited 11 mins ago

























      answered 14 hours ago









      Henrik Schumacher

      43.5k263129




      43.5k263129











      • Only the middle approach is robust. +1 for it.
        – Bob Hanlon
        9 hours ago
















      • Only the middle approach is robust. +1 for it.
        – Bob Hanlon
        9 hours ago















      Only the middle approach is robust. +1 for it.
      – Bob Hanlon
      9 hours ago




      Only the middle approach is robust. +1 for it.
      – Bob Hanlon
      9 hours ago










      up vote
      4
      down vote













      As it turns out, there is a special function for taking every other element called DownSample:



      list = Range[20];
      a = Downsample[list, 2]
      b = Downsample[list, 2, 2]


      a has all the odd values, b has all the evens.






      share|improve this answer
























        up vote
        4
        down vote













        As it turns out, there is a special function for taking every other element called DownSample:



        list = Range[20];
        a = Downsample[list, 2]
        b = Downsample[list, 2, 2]


        a has all the odd values, b has all the evens.






        share|improve this answer






















          up vote
          4
          down vote










          up vote
          4
          down vote









          As it turns out, there is a special function for taking every other element called DownSample:



          list = Range[20];
          a = Downsample[list, 2]
          b = Downsample[list, 2, 2]


          a has all the odd values, b has all the evens.






          share|improve this answer












          As it turns out, there is a special function for taking every other element called DownSample:



          list = Range[20];
          a = Downsample[list, 2]
          b = Downsample[list, 2, 2]


          a has all the odd values, b has all the evens.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 14 hours ago









          bill s

          52k375146




          52k375146




















              up vote
              2
              down vote













              list = Range@20; 


              here are alternatives



              TakeDrop[list, 1, -1, 2] 



              1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 2, 4, 6, 8, 10, 12, 14, 16, 18,
              20




              and here is random separation



              d = RandomSample[s, Length@list/2], Complement[list, d] 



              4, 12, 5, 19, 3, 11, 17, 16, 6, 10, 1, 2, 7, 8, 9, 13, 14, 15, 18,
              20







              share|improve this answer
























                up vote
                2
                down vote













                list = Range@20; 


                here are alternatives



                TakeDrop[list, 1, -1, 2] 



                1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 2, 4, 6, 8, 10, 12, 14, 16, 18,
                20




                and here is random separation



                d = RandomSample[s, Length@list/2], Complement[list, d] 



                4, 12, 5, 19, 3, 11, 17, 16, 6, 10, 1, 2, 7, 8, 9, 13, 14, 15, 18,
                20







                share|improve this answer






















                  up vote
                  2
                  down vote










                  up vote
                  2
                  down vote









                  list = Range@20; 


                  here are alternatives



                  TakeDrop[list, 1, -1, 2] 



                  1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 2, 4, 6, 8, 10, 12, 14, 16, 18,
                  20




                  and here is random separation



                  d = RandomSample[s, Length@list/2], Complement[list, d] 



                  4, 12, 5, 19, 3, 11, 17, 16, 6, 10, 1, 2, 7, 8, 9, 13, 14, 15, 18,
                  20







                  share|improve this answer












                  list = Range@20; 


                  here are alternatives



                  TakeDrop[list, 1, -1, 2] 



                  1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 2, 4, 6, 8, 10, 12, 14, 16, 18,
                  20




                  and here is random separation



                  d = RandomSample[s, Length@list/2], Complement[list, d] 



                  4, 12, 5, 19, 3, 11, 17, 16, 6, 10, 1, 2, 7, 8, 9, 13, 14, 15, 18,
                  20








                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 11 hours ago









                  J42161217

                  2,257218




                  2,257218




















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









                       

                      draft saved


                      draft discarded


















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












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











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













                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f185103%2fhow-to-divide-one-list-into-2-using-a-for-loop%23new-answer', 'question_page');

                      );

                      Post as a guest













































































                      Comments

                      Popular posts from this blog

                      White Anglo-Saxon Protestant

                      BuddyTV

                      Conflict (narrative)