how can I run two function in for loop step by step?












0














fruits=["apple","banana","melon"]
for fruit in fruits:
print fruit #print1
print len(fruit) #print2


I know output of this script will be like below



apple
5
banana
6
melon
5


but I want to run this script like

#print1 > #print1 > #print1 > #print2 > #print2 > #print2



so the output will be like



apple
banana
melon
5
6
5


Is there any good way for this?
(point is that I want to use for function one time)
(Sorry for bad expanation)










share|improve this question





























    0














    fruits=["apple","banana","melon"]
    for fruit in fruits:
    print fruit #print1
    print len(fruit) #print2


    I know output of this script will be like below



    apple
    5
    banana
    6
    melon
    5


    but I want to run this script like

    #print1 > #print1 > #print1 > #print2 > #print2 > #print2



    so the output will be like



    apple
    banana
    melon
    5
    6
    5


    Is there any good way for this?
    (point is that I want to use for function one time)
    (Sorry for bad expanation)










    share|improve this question



























      0












      0








      0







      fruits=["apple","banana","melon"]
      for fruit in fruits:
      print fruit #print1
      print len(fruit) #print2


      I know output of this script will be like below



      apple
      5
      banana
      6
      melon
      5


      but I want to run this script like

      #print1 > #print1 > #print1 > #print2 > #print2 > #print2



      so the output will be like



      apple
      banana
      melon
      5
      6
      5


      Is there any good way for this?
      (point is that I want to use for function one time)
      (Sorry for bad expanation)










      share|improve this question















      fruits=["apple","banana","melon"]
      for fruit in fruits:
      print fruit #print1
      print len(fruit) #print2


      I know output of this script will be like below



      apple
      5
      banana
      6
      melon
      5


      but I want to run this script like

      #print1 > #print1 > #print1 > #print2 > #print2 > #print2



      so the output will be like



      apple
      banana
      melon
      5
      6
      5


      Is there any good way for this?
      (point is that I want to use for function one time)
      (Sorry for bad expanation)







      python for-loop






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 13 '18 at 3:19

























      asked Nov 13 '18 at 3:11









      tehoo

      344




      344
























          7 Answers
          7






          active

          oldest

          votes


















          1














          The simplest way to do this is to iterate the loop twice:



          fruits = ["apple", "banana", "melon"]

          for _ in range(2):
          for fruit in fruits:
          print fruit


          which gives:



          apple
          banana
          melon
          apple
          banana
          melon


          This avoids duplicating the list object, avoids duplicating code, and scales easily.



          Edit: to iterate over two different functions, separately, you can just iterate over the list twice:



          fruits = ["apple", "banana", "melon"]

          for fruit in fruits:
          print fruit
          for fruit in fruits:
          print len(fruit)


          For more complex situations you could also make a list of your functions and iterate through those:



          def print_name(item):
          print(string)

          def print_name_length(item):
          print(len(item))

          fruits = ["apple", "banana", "melon"]

          for func in (print_name, print_name_length):
          for fruit in fruits:
          func(fruit)





          share|improve this answer























          • thanks ! but what if two functions in the loop are different? is there any other way?
            – tehoo
            Nov 13 '18 at 3:27










          • No problem. See the edit for how to call two different functions.
            – 101
            Nov 13 '18 at 3:37










          • Thank you so much !! I thought there must be a way using only one for loop but I think there is no way like that!!
            – tehoo
            Nov 13 '18 at 3:54



















          1














          You can create a list of functions you want to call and loop over them first, and then loop over fruits:



          fruits = ["apple", "banana", "melon"]

          for func in [lambda x: x, len]:
          for fruit in fruits:
          print(func(fruit))


          Output



          apple
          banana
          melon
          5
          6
          5





          share|improve this answer































            0














            It looks like you could just loop through the same list twice to achieve the desired logging:



            for i in range(2):
            for fruit in fruits:
            print(fruit)





            share|improve this answer





























              0














              You can use generator expressions:



              fruits=["apple","banana","melon"]

              def getfruits(fruits):
              for f in fruits:
              print(f)
              yield str(len(f)) + 'n'

              print(*list(getfruits(fruits)))


              Output:



              apple
              banana
              melon
              5
              6
              5





              share|improve this answer





























                0














                fruits=["apple","banana","melon"]

                for lists in (fruits, (len(fruit) for fruit in fruits)):
                for each_list in lists:
                print(each_list)


                I make a loop iterating over two lists; fruits and a list containing the lengths of the fruit in fruits.



                Then I print out each list.



                This could probably be more memory efficient, but this makes the most sense to me. I was going for readability.






                share|improve this answer





























                  -1














                  You can just run the same loop twice.



                  fruits = ["apple","banana","melon"]
                  for f in fruits:
                  print f
                  for f in fruits:
                  print f


                  Which will get you the result. It may be ugly but it gets the job done.






                  share|improve this answer





























                    -1














                    There is no way to do this in one loop. Is there any reason why this would not work:



                    for fruit in fruits:
                    print fruit #print1
                    for fruit in fruits:
                    print fruit #print2





                    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%2f53273233%2fhow-can-i-run-two-function-in-for-loop-step-by-step%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown

























                      7 Answers
                      7






                      active

                      oldest

                      votes








                      7 Answers
                      7






                      active

                      oldest

                      votes









                      active

                      oldest

                      votes






                      active

                      oldest

                      votes









                      1














                      The simplest way to do this is to iterate the loop twice:



                      fruits = ["apple", "banana", "melon"]

                      for _ in range(2):
                      for fruit in fruits:
                      print fruit


                      which gives:



                      apple
                      banana
                      melon
                      apple
                      banana
                      melon


                      This avoids duplicating the list object, avoids duplicating code, and scales easily.



                      Edit: to iterate over two different functions, separately, you can just iterate over the list twice:



                      fruits = ["apple", "banana", "melon"]

                      for fruit in fruits:
                      print fruit
                      for fruit in fruits:
                      print len(fruit)


                      For more complex situations you could also make a list of your functions and iterate through those:



                      def print_name(item):
                      print(string)

                      def print_name_length(item):
                      print(len(item))

                      fruits = ["apple", "banana", "melon"]

                      for func in (print_name, print_name_length):
                      for fruit in fruits:
                      func(fruit)





                      share|improve this answer























                      • thanks ! but what if two functions in the loop are different? is there any other way?
                        – tehoo
                        Nov 13 '18 at 3:27










                      • No problem. See the edit for how to call two different functions.
                        – 101
                        Nov 13 '18 at 3:37










                      • Thank you so much !! I thought there must be a way using only one for loop but I think there is no way like that!!
                        – tehoo
                        Nov 13 '18 at 3:54
















                      1














                      The simplest way to do this is to iterate the loop twice:



                      fruits = ["apple", "banana", "melon"]

                      for _ in range(2):
                      for fruit in fruits:
                      print fruit


                      which gives:



                      apple
                      banana
                      melon
                      apple
                      banana
                      melon


                      This avoids duplicating the list object, avoids duplicating code, and scales easily.



                      Edit: to iterate over two different functions, separately, you can just iterate over the list twice:



                      fruits = ["apple", "banana", "melon"]

                      for fruit in fruits:
                      print fruit
                      for fruit in fruits:
                      print len(fruit)


                      For more complex situations you could also make a list of your functions and iterate through those:



                      def print_name(item):
                      print(string)

                      def print_name_length(item):
                      print(len(item))

                      fruits = ["apple", "banana", "melon"]

                      for func in (print_name, print_name_length):
                      for fruit in fruits:
                      func(fruit)





                      share|improve this answer























                      • thanks ! but what if two functions in the loop are different? is there any other way?
                        – tehoo
                        Nov 13 '18 at 3:27










                      • No problem. See the edit for how to call two different functions.
                        – 101
                        Nov 13 '18 at 3:37










                      • Thank you so much !! I thought there must be a way using only one for loop but I think there is no way like that!!
                        – tehoo
                        Nov 13 '18 at 3:54














                      1












                      1








                      1






                      The simplest way to do this is to iterate the loop twice:



                      fruits = ["apple", "banana", "melon"]

                      for _ in range(2):
                      for fruit in fruits:
                      print fruit


                      which gives:



                      apple
                      banana
                      melon
                      apple
                      banana
                      melon


                      This avoids duplicating the list object, avoids duplicating code, and scales easily.



                      Edit: to iterate over two different functions, separately, you can just iterate over the list twice:



                      fruits = ["apple", "banana", "melon"]

                      for fruit in fruits:
                      print fruit
                      for fruit in fruits:
                      print len(fruit)


                      For more complex situations you could also make a list of your functions and iterate through those:



                      def print_name(item):
                      print(string)

                      def print_name_length(item):
                      print(len(item))

                      fruits = ["apple", "banana", "melon"]

                      for func in (print_name, print_name_length):
                      for fruit in fruits:
                      func(fruit)





                      share|improve this answer














                      The simplest way to do this is to iterate the loop twice:



                      fruits = ["apple", "banana", "melon"]

                      for _ in range(2):
                      for fruit in fruits:
                      print fruit


                      which gives:



                      apple
                      banana
                      melon
                      apple
                      banana
                      melon


                      This avoids duplicating the list object, avoids duplicating code, and scales easily.



                      Edit: to iterate over two different functions, separately, you can just iterate over the list twice:



                      fruits = ["apple", "banana", "melon"]

                      for fruit in fruits:
                      print fruit
                      for fruit in fruits:
                      print len(fruit)


                      For more complex situations you could also make a list of your functions and iterate through those:



                      def print_name(item):
                      print(string)

                      def print_name_length(item):
                      print(len(item))

                      fruits = ["apple", "banana", "melon"]

                      for func in (print_name, print_name_length):
                      for fruit in fruits:
                      func(fruit)






                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Nov 14 '18 at 23:48

























                      answered Nov 13 '18 at 3:16









                      101

                      5,26422146




                      5,26422146












                      • thanks ! but what if two functions in the loop are different? is there any other way?
                        – tehoo
                        Nov 13 '18 at 3:27










                      • No problem. See the edit for how to call two different functions.
                        – 101
                        Nov 13 '18 at 3:37










                      • Thank you so much !! I thought there must be a way using only one for loop but I think there is no way like that!!
                        – tehoo
                        Nov 13 '18 at 3:54


















                      • thanks ! but what if two functions in the loop are different? is there any other way?
                        – tehoo
                        Nov 13 '18 at 3:27










                      • No problem. See the edit for how to call two different functions.
                        – 101
                        Nov 13 '18 at 3:37










                      • Thank you so much !! I thought there must be a way using only one for loop but I think there is no way like that!!
                        – tehoo
                        Nov 13 '18 at 3:54
















                      thanks ! but what if two functions in the loop are different? is there any other way?
                      – tehoo
                      Nov 13 '18 at 3:27




                      thanks ! but what if two functions in the loop are different? is there any other way?
                      – tehoo
                      Nov 13 '18 at 3:27












                      No problem. See the edit for how to call two different functions.
                      – 101
                      Nov 13 '18 at 3:37




                      No problem. See the edit for how to call two different functions.
                      – 101
                      Nov 13 '18 at 3:37












                      Thank you so much !! I thought there must be a way using only one for loop but I think there is no way like that!!
                      – tehoo
                      Nov 13 '18 at 3:54




                      Thank you so much !! I thought there must be a way using only one for loop but I think there is no way like that!!
                      – tehoo
                      Nov 13 '18 at 3:54













                      1














                      You can create a list of functions you want to call and loop over them first, and then loop over fruits:



                      fruits = ["apple", "banana", "melon"]

                      for func in [lambda x: x, len]:
                      for fruit in fruits:
                      print(func(fruit))


                      Output



                      apple
                      banana
                      melon
                      5
                      6
                      5





                      share|improve this answer




























                        1














                        You can create a list of functions you want to call and loop over them first, and then loop over fruits:



                        fruits = ["apple", "banana", "melon"]

                        for func in [lambda x: x, len]:
                        for fruit in fruits:
                        print(func(fruit))


                        Output



                        apple
                        banana
                        melon
                        5
                        6
                        5





                        share|improve this answer


























                          1












                          1








                          1






                          You can create a list of functions you want to call and loop over them first, and then loop over fruits:



                          fruits = ["apple", "banana", "melon"]

                          for func in [lambda x: x, len]:
                          for fruit in fruits:
                          print(func(fruit))


                          Output



                          apple
                          banana
                          melon
                          5
                          6
                          5





                          share|improve this answer














                          You can create a list of functions you want to call and loop over them first, and then loop over fruits:



                          fruits = ["apple", "banana", "melon"]

                          for func in [lambda x: x, len]:
                          for fruit in fruits:
                          print(func(fruit))


                          Output



                          apple
                          banana
                          melon
                          5
                          6
                          5






                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Nov 13 '18 at 3:30

























                          answered Nov 13 '18 at 3:15









                          slider

                          8,07011129




                          8,07011129























                              0














                              It looks like you could just loop through the same list twice to achieve the desired logging:



                              for i in range(2):
                              for fruit in fruits:
                              print(fruit)





                              share|improve this answer


























                                0














                                It looks like you could just loop through the same list twice to achieve the desired logging:



                                for i in range(2):
                                for fruit in fruits:
                                print(fruit)





                                share|improve this answer
























                                  0












                                  0








                                  0






                                  It looks like you could just loop through the same list twice to achieve the desired logging:



                                  for i in range(2):
                                  for fruit in fruits:
                                  print(fruit)





                                  share|improve this answer












                                  It looks like you could just loop through the same list twice to achieve the desired logging:



                                  for i in range(2):
                                  for fruit in fruits:
                                  print(fruit)






                                  share|improve this answer












                                  share|improve this answer



                                  share|improve this answer










                                  answered Nov 13 '18 at 3:16









                                  Riley Steele Parsons

                                  23116




                                  23116























                                      0














                                      You can use generator expressions:



                                      fruits=["apple","banana","melon"]

                                      def getfruits(fruits):
                                      for f in fruits:
                                      print(f)
                                      yield str(len(f)) + 'n'

                                      print(*list(getfruits(fruits)))


                                      Output:



                                      apple
                                      banana
                                      melon
                                      5
                                      6
                                      5





                                      share|improve this answer


























                                        0














                                        You can use generator expressions:



                                        fruits=["apple","banana","melon"]

                                        def getfruits(fruits):
                                        for f in fruits:
                                        print(f)
                                        yield str(len(f)) + 'n'

                                        print(*list(getfruits(fruits)))


                                        Output:



                                        apple
                                        banana
                                        melon
                                        5
                                        6
                                        5





                                        share|improve this answer
























                                          0












                                          0








                                          0






                                          You can use generator expressions:



                                          fruits=["apple","banana","melon"]

                                          def getfruits(fruits):
                                          for f in fruits:
                                          print(f)
                                          yield str(len(f)) + 'n'

                                          print(*list(getfruits(fruits)))


                                          Output:



                                          apple
                                          banana
                                          melon
                                          5
                                          6
                                          5





                                          share|improve this answer












                                          You can use generator expressions:



                                          fruits=["apple","banana","melon"]

                                          def getfruits(fruits):
                                          for f in fruits:
                                          print(f)
                                          yield str(len(f)) + 'n'

                                          print(*list(getfruits(fruits)))


                                          Output:



                                          apple
                                          banana
                                          melon
                                          5
                                          6
                                          5






                                          share|improve this answer












                                          share|improve this answer



                                          share|improve this answer










                                          answered Nov 13 '18 at 3:56









                                          Rishabh Mishra

                                          370210




                                          370210























                                              0














                                              fruits=["apple","banana","melon"]

                                              for lists in (fruits, (len(fruit) for fruit in fruits)):
                                              for each_list in lists:
                                              print(each_list)


                                              I make a loop iterating over two lists; fruits and a list containing the lengths of the fruit in fruits.



                                              Then I print out each list.



                                              This could probably be more memory efficient, but this makes the most sense to me. I was going for readability.






                                              share|improve this answer


























                                                0














                                                fruits=["apple","banana","melon"]

                                                for lists in (fruits, (len(fruit) for fruit in fruits)):
                                                for each_list in lists:
                                                print(each_list)


                                                I make a loop iterating over two lists; fruits and a list containing the lengths of the fruit in fruits.



                                                Then I print out each list.



                                                This could probably be more memory efficient, but this makes the most sense to me. I was going for readability.






                                                share|improve this answer
























                                                  0












                                                  0








                                                  0






                                                  fruits=["apple","banana","melon"]

                                                  for lists in (fruits, (len(fruit) for fruit in fruits)):
                                                  for each_list in lists:
                                                  print(each_list)


                                                  I make a loop iterating over two lists; fruits and a list containing the lengths of the fruit in fruits.



                                                  Then I print out each list.



                                                  This could probably be more memory efficient, but this makes the most sense to me. I was going for readability.






                                                  share|improve this answer












                                                  fruits=["apple","banana","melon"]

                                                  for lists in (fruits, (len(fruit) for fruit in fruits)):
                                                  for each_list in lists:
                                                  print(each_list)


                                                  I make a loop iterating over two lists; fruits and a list containing the lengths of the fruit in fruits.



                                                  Then I print out each list.



                                                  This could probably be more memory efficient, but this makes the most sense to me. I was going for readability.







                                                  share|improve this answer












                                                  share|improve this answer



                                                  share|improve this answer










                                                  answered Nov 13 '18 at 4:25









                                                  Alex White

                                                  1738




                                                  1738























                                                      -1














                                                      You can just run the same loop twice.



                                                      fruits = ["apple","banana","melon"]
                                                      for f in fruits:
                                                      print f
                                                      for f in fruits:
                                                      print f


                                                      Which will get you the result. It may be ugly but it gets the job done.






                                                      share|improve this answer


























                                                        -1














                                                        You can just run the same loop twice.



                                                        fruits = ["apple","banana","melon"]
                                                        for f in fruits:
                                                        print f
                                                        for f in fruits:
                                                        print f


                                                        Which will get you the result. It may be ugly but it gets the job done.






                                                        share|improve this answer
























                                                          -1












                                                          -1








                                                          -1






                                                          You can just run the same loop twice.



                                                          fruits = ["apple","banana","melon"]
                                                          for f in fruits:
                                                          print f
                                                          for f in fruits:
                                                          print f


                                                          Which will get you the result. It may be ugly but it gets the job done.






                                                          share|improve this answer












                                                          You can just run the same loop twice.



                                                          fruits = ["apple","banana","melon"]
                                                          for f in fruits:
                                                          print f
                                                          for f in fruits:
                                                          print f


                                                          Which will get you the result. It may be ugly but it gets the job done.







                                                          share|improve this answer












                                                          share|improve this answer



                                                          share|improve this answer










                                                          answered Nov 13 '18 at 3:15









                                                          David White

                                                          3901214




                                                          3901214























                                                              -1














                                                              There is no way to do this in one loop. Is there any reason why this would not work:



                                                              for fruit in fruits:
                                                              print fruit #print1
                                                              for fruit in fruits:
                                                              print fruit #print2





                                                              share|improve this answer


























                                                                -1














                                                                There is no way to do this in one loop. Is there any reason why this would not work:



                                                                for fruit in fruits:
                                                                print fruit #print1
                                                                for fruit in fruits:
                                                                print fruit #print2





                                                                share|improve this answer
























                                                                  -1












                                                                  -1








                                                                  -1






                                                                  There is no way to do this in one loop. Is there any reason why this would not work:



                                                                  for fruit in fruits:
                                                                  print fruit #print1
                                                                  for fruit in fruits:
                                                                  print fruit #print2





                                                                  share|improve this answer












                                                                  There is no way to do this in one loop. Is there any reason why this would not work:



                                                                  for fruit in fruits:
                                                                  print fruit #print1
                                                                  for fruit in fruits:
                                                                  print fruit #print2






                                                                  share|improve this answer












                                                                  share|improve this answer



                                                                  share|improve this answer










                                                                  answered Nov 13 '18 at 3:15









                                                                  The Zach Man

                                                                  1146




                                                                  1146






























                                                                      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%2f53273233%2fhow-can-i-run-two-function-in-for-loop-step-by-step%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

                                                                      List item for chat from Array inside array React Native

                                                                      Thiostrepton

                                                                      Caerphilly