Iterating through every other element within list recursively










-1














I have been trying to figure out a clear and concise method to iterate through elements in a list in a form of recursive loop.



For example, if I have a list:



My_list = [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]


I would like to extract every other element from the list.



New_list = [2, 6, 10, 14, 18]


I then want to take the discarded values and take every second of those. And so on, recursively.



Since my first run through the list ended at 18, I will skip over 20 and go back to the beginning of the list as I want to extract every other elements.



My_list = [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]

# Elements not been used after first operation = [4, 8, 12, 16, 20]

New_list = [2, 6, 10, 14, 18, 4, 12, 20, 8, 16] # desired output


What methods can I use to loop within a list?










share|improve this question























  • Have you written some code that we can look at?
    – L3viathan
    Nov 12 at 12:18










  • My_list[::2] ....
    – JBernardo
    Nov 12 at 12:18










  • Do you also need to extract a list for Elements not been used ? Is [2, 6, 10, 14, 18, 4, 12, 20, 16, 8] your desired output?
    – jpp
    Nov 12 at 12:19







  • 1




    @jpp yes I already know how to extract every two elements but it is my goal to loop over the list and extract again from elements not been used. Indeed [2, 6, 10, 14, 18, 4, 12, 20, 16, 8] is my desired output
    – V Anon
    Nov 12 at 12:24






  • 1




    @JBernardo yes I am aware of the method of extracting every other two elements, but would there be a way to loop through the list again?
    – V Anon
    Nov 12 at 12:25















-1














I have been trying to figure out a clear and concise method to iterate through elements in a list in a form of recursive loop.



For example, if I have a list:



My_list = [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]


I would like to extract every other element from the list.



New_list = [2, 6, 10, 14, 18]


I then want to take the discarded values and take every second of those. And so on, recursively.



Since my first run through the list ended at 18, I will skip over 20 and go back to the beginning of the list as I want to extract every other elements.



My_list = [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]

# Elements not been used after first operation = [4, 8, 12, 16, 20]

New_list = [2, 6, 10, 14, 18, 4, 12, 20, 8, 16] # desired output


What methods can I use to loop within a list?










share|improve this question























  • Have you written some code that we can look at?
    – L3viathan
    Nov 12 at 12:18










  • My_list[::2] ....
    – JBernardo
    Nov 12 at 12:18










  • Do you also need to extract a list for Elements not been used ? Is [2, 6, 10, 14, 18, 4, 12, 20, 16, 8] your desired output?
    – jpp
    Nov 12 at 12:19







  • 1




    @jpp yes I already know how to extract every two elements but it is my goal to loop over the list and extract again from elements not been used. Indeed [2, 6, 10, 14, 18, 4, 12, 20, 16, 8] is my desired output
    – V Anon
    Nov 12 at 12:24






  • 1




    @JBernardo yes I am aware of the method of extracting every other two elements, but would there be a way to loop through the list again?
    – V Anon
    Nov 12 at 12:25













-1












-1








-1







I have been trying to figure out a clear and concise method to iterate through elements in a list in a form of recursive loop.



For example, if I have a list:



My_list = [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]


I would like to extract every other element from the list.



New_list = [2, 6, 10, 14, 18]


I then want to take the discarded values and take every second of those. And so on, recursively.



Since my first run through the list ended at 18, I will skip over 20 and go back to the beginning of the list as I want to extract every other elements.



My_list = [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]

# Elements not been used after first operation = [4, 8, 12, 16, 20]

New_list = [2, 6, 10, 14, 18, 4, 12, 20, 8, 16] # desired output


What methods can I use to loop within a list?










share|improve this question















I have been trying to figure out a clear and concise method to iterate through elements in a list in a form of recursive loop.



For example, if I have a list:



My_list = [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]


I would like to extract every other element from the list.



New_list = [2, 6, 10, 14, 18]


I then want to take the discarded values and take every second of those. And so on, recursively.



Since my first run through the list ended at 18, I will skip over 20 and go back to the beginning of the list as I want to extract every other elements.



My_list = [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]

# Elements not been used after first operation = [4, 8, 12, 16, 20]

New_list = [2, 6, 10, 14, 18, 4, 12, 20, 8, 16] # desired output


What methods can I use to loop within a list?







python python-3.x python-2.7 list recursion






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 at 12:44









jpp

90.8k2052101




90.8k2052101










asked Nov 12 at 12:15









V Anon

2066




2066











  • Have you written some code that we can look at?
    – L3viathan
    Nov 12 at 12:18










  • My_list[::2] ....
    – JBernardo
    Nov 12 at 12:18










  • Do you also need to extract a list for Elements not been used ? Is [2, 6, 10, 14, 18, 4, 12, 20, 16, 8] your desired output?
    – jpp
    Nov 12 at 12:19







  • 1




    @jpp yes I already know how to extract every two elements but it is my goal to loop over the list and extract again from elements not been used. Indeed [2, 6, 10, 14, 18, 4, 12, 20, 16, 8] is my desired output
    – V Anon
    Nov 12 at 12:24






  • 1




    @JBernardo yes I am aware of the method of extracting every other two elements, but would there be a way to loop through the list again?
    – V Anon
    Nov 12 at 12:25
















  • Have you written some code that we can look at?
    – L3viathan
    Nov 12 at 12:18










  • My_list[::2] ....
    – JBernardo
    Nov 12 at 12:18










  • Do you also need to extract a list for Elements not been used ? Is [2, 6, 10, 14, 18, 4, 12, 20, 16, 8] your desired output?
    – jpp
    Nov 12 at 12:19







  • 1




    @jpp yes I already know how to extract every two elements but it is my goal to loop over the list and extract again from elements not been used. Indeed [2, 6, 10, 14, 18, 4, 12, 20, 16, 8] is my desired output
    – V Anon
    Nov 12 at 12:24






  • 1




    @JBernardo yes I am aware of the method of extracting every other two elements, but would there be a way to loop through the list again?
    – V Anon
    Nov 12 at 12:25















Have you written some code that we can look at?
– L3viathan
Nov 12 at 12:18




Have you written some code that we can look at?
– L3viathan
Nov 12 at 12:18












My_list[::2] ....
– JBernardo
Nov 12 at 12:18




My_list[::2] ....
– JBernardo
Nov 12 at 12:18












Do you also need to extract a list for Elements not been used ? Is [2, 6, 10, 14, 18, 4, 12, 20, 16, 8] your desired output?
– jpp
Nov 12 at 12:19





Do you also need to extract a list for Elements not been used ? Is [2, 6, 10, 14, 18, 4, 12, 20, 16, 8] your desired output?
– jpp
Nov 12 at 12:19





1




1




@jpp yes I already know how to extract every two elements but it is my goal to loop over the list and extract again from elements not been used. Indeed [2, 6, 10, 14, 18, 4, 12, 20, 16, 8] is my desired output
– V Anon
Nov 12 at 12:24




@jpp yes I already know how to extract every two elements but it is my goal to loop over the list and extract again from elements not been used. Indeed [2, 6, 10, 14, 18, 4, 12, 20, 16, 8] is my desired output
– V Anon
Nov 12 at 12:24




1




1




@JBernardo yes I am aware of the method of extracting every other two elements, but would there be a way to loop through the list again?
– V Anon
Nov 12 at 12:25




@JBernardo yes I am aware of the method of extracting every other two elements, but would there be a way to loop through the list again?
– V Anon
Nov 12 at 12:25












3 Answers
3






active

oldest

votes


















0














Yes, you can use a while loop with a generator:



L = [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]

def recursive_odds(x):
while x:
yield from x[::2]
x = x[1::2]

res = list(recursive_odds(L))

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





share|improve this answer






























    2














    you can use python's really cool slicing syntax:



    new_list=my_list[::2]



    to get every other element.



    It means new_list is my_list from the begining to the end with a stride of 2



    then your ramainder elements are elements_not_used = [item for item in my_list if item not in new_list] and you can just continue until len(my_list)<2






    share|improve this answer






















    • This doesn't actually answer OP's question.
      – jpp
      Nov 12 at 12:38


















    1














    using while loop you can do this:



    final=
    while len(mylist)>0:
    final.extend(mylist[::2])
    mylist=mylist[1::2]
    print(final)





    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%2f53262001%2fiterating-through-every-other-element-within-list-recursively%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      Yes, you can use a while loop with a generator:



      L = [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]

      def recursive_odds(x):
      while x:
      yield from x[::2]
      x = x[1::2]

      res = list(recursive_odds(L))

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





      share|improve this answer



























        0














        Yes, you can use a while loop with a generator:



        L = [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]

        def recursive_odds(x):
        while x:
        yield from x[::2]
        x = x[1::2]

        res = list(recursive_odds(L))

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





        share|improve this answer

























          0












          0








          0






          Yes, you can use a while loop with a generator:



          L = [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]

          def recursive_odds(x):
          while x:
          yield from x[::2]
          x = x[1::2]

          res = list(recursive_odds(L))

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





          share|improve this answer














          Yes, you can use a while loop with a generator:



          L = [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]

          def recursive_odds(x):
          while x:
          yield from x[::2]
          x = x[1::2]

          res = list(recursive_odds(L))

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






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 12 at 12:43

























          answered Nov 12 at 12:33









          jpp

          90.8k2052101




          90.8k2052101























              2














              you can use python's really cool slicing syntax:



              new_list=my_list[::2]



              to get every other element.



              It means new_list is my_list from the begining to the end with a stride of 2



              then your ramainder elements are elements_not_used = [item for item in my_list if item not in new_list] and you can just continue until len(my_list)<2






              share|improve this answer






















              • This doesn't actually answer OP's question.
                – jpp
                Nov 12 at 12:38















              2














              you can use python's really cool slicing syntax:



              new_list=my_list[::2]



              to get every other element.



              It means new_list is my_list from the begining to the end with a stride of 2



              then your ramainder elements are elements_not_used = [item for item in my_list if item not in new_list] and you can just continue until len(my_list)<2






              share|improve this answer






















              • This doesn't actually answer OP's question.
                – jpp
                Nov 12 at 12:38













              2












              2








              2






              you can use python's really cool slicing syntax:



              new_list=my_list[::2]



              to get every other element.



              It means new_list is my_list from the begining to the end with a stride of 2



              then your ramainder elements are elements_not_used = [item for item in my_list if item not in new_list] and you can just continue until len(my_list)<2






              share|improve this answer














              you can use python's really cool slicing syntax:



              new_list=my_list[::2]



              to get every other element.



              It means new_list is my_list from the begining to the end with a stride of 2



              then your ramainder elements are elements_not_used = [item for item in my_list if item not in new_list] and you can just continue until len(my_list)<2







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Nov 12 at 12:20

























              answered Nov 12 at 12:18









              vencaslac

              1,002217




              1,002217











              • This doesn't actually answer OP's question.
                – jpp
                Nov 12 at 12:38
















              • This doesn't actually answer OP's question.
                – jpp
                Nov 12 at 12:38















              This doesn't actually answer OP's question.
              – jpp
              Nov 12 at 12:38




              This doesn't actually answer OP's question.
              – jpp
              Nov 12 at 12:38











              1














              using while loop you can do this:



              final=
              while len(mylist)>0:
              final.extend(mylist[::2])
              mylist=mylist[1::2]
              print(final)





              share|improve this answer



























                1














                using while loop you can do this:



                final=
                while len(mylist)>0:
                final.extend(mylist[::2])
                mylist=mylist[1::2]
                print(final)





                share|improve this answer

























                  1












                  1








                  1






                  using while loop you can do this:



                  final=
                  while len(mylist)>0:
                  final.extend(mylist[::2])
                  mylist=mylist[1::2]
                  print(final)





                  share|improve this answer














                  using while loop you can do this:



                  final=
                  while len(mylist)>0:
                  final.extend(mylist[::2])
                  mylist=mylist[1::2]
                  print(final)






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 12 at 13:02









                  vencaslac

                  1,002217




                  1,002217










                  answered Nov 12 at 12:42









                  Gautham Mohan

                  213




                  213



























                      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.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • 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%2f53262001%2fiterating-through-every-other-element-within-list-recursively%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







                      這個網誌中的熱門文章

                      How to read a connectionString WITH PROVIDER in .NET Core?

                      Node.js Script on GitHub Pages or Amazon S3

                      Museum of Modern and Contemporary Art of Trento and Rovereto