How can I return multiple values like print in Python 3?












-1















For example, if I use print it gives me 101 238 157 and None.



i = 0
while i < 3:
champion = matchList['matches'][i]['champion']
i = i + 1
print(champion)


But if I use RETURN it only returns 101.
So what can I do?










share|improve this question




















  • 3





    return will stop the execution of your while loop (assuming that you actual code is valid and that example is actually in a function) and return the current value of champion. You will either need to collect all values of champion that you want in a list or similar before returning the list, or use a generator function and yield the results one at a time.

    – SuperShoot
    Nov 25 '18 at 11:13













  • Gather all results in a list and return the list after the loop.

    – Matthias
    Nov 25 '18 at 11:14
















-1















For example, if I use print it gives me 101 238 157 and None.



i = 0
while i < 3:
champion = matchList['matches'][i]['champion']
i = i + 1
print(champion)


But if I use RETURN it only returns 101.
So what can I do?










share|improve this question




















  • 3





    return will stop the execution of your while loop (assuming that you actual code is valid and that example is actually in a function) and return the current value of champion. You will either need to collect all values of champion that you want in a list or similar before returning the list, or use a generator function and yield the results one at a time.

    – SuperShoot
    Nov 25 '18 at 11:13













  • Gather all results in a list and return the list after the loop.

    – Matthias
    Nov 25 '18 at 11:14














-1












-1








-1








For example, if I use print it gives me 101 238 157 and None.



i = 0
while i < 3:
champion = matchList['matches'][i]['champion']
i = i + 1
print(champion)


But if I use RETURN it only returns 101.
So what can I do?










share|improve this question
















For example, if I use print it gives me 101 238 157 and None.



i = 0
while i < 3:
champion = matchList['matches'][i]['champion']
i = i + 1
print(champion)


But if I use RETURN it only returns 101.
So what can I do?







python python-3.5






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 25 '18 at 14:45









Pavan Kumar T S

565418




565418










asked Nov 25 '18 at 11:10









BardBard

82




82








  • 3





    return will stop the execution of your while loop (assuming that you actual code is valid and that example is actually in a function) and return the current value of champion. You will either need to collect all values of champion that you want in a list or similar before returning the list, or use a generator function and yield the results one at a time.

    – SuperShoot
    Nov 25 '18 at 11:13













  • Gather all results in a list and return the list after the loop.

    – Matthias
    Nov 25 '18 at 11:14














  • 3





    return will stop the execution of your while loop (assuming that you actual code is valid and that example is actually in a function) and return the current value of champion. You will either need to collect all values of champion that you want in a list or similar before returning the list, or use a generator function and yield the results one at a time.

    – SuperShoot
    Nov 25 '18 at 11:13













  • Gather all results in a list and return the list after the loop.

    – Matthias
    Nov 25 '18 at 11:14








3




3





return will stop the execution of your while loop (assuming that you actual code is valid and that example is actually in a function) and return the current value of champion. You will either need to collect all values of champion that you want in a list or similar before returning the list, or use a generator function and yield the results one at a time.

– SuperShoot
Nov 25 '18 at 11:13







return will stop the execution of your while loop (assuming that you actual code is valid and that example is actually in a function) and return the current value of champion. You will either need to collect all values of champion that you want in a list or similar before returning the list, or use a generator function and yield the results one at a time.

– SuperShoot
Nov 25 '18 at 11:13















Gather all results in a list and return the list after the loop.

– Matthias
Nov 25 '18 at 11:14





Gather all results in a list and return the list after the loop.

– Matthias
Nov 25 '18 at 11:14












4 Answers
4






active

oldest

votes


















0














return can have only one value, (that can be an object like a list or something else)... why? Just because return is the value that assume the function. In the moment you do an assignment of a function for example



def champs()
return MJ KD LBJ

champ = champs()


In this way the number should be MJ, KD and LBJ at the same time... impossible conceptually. But we can return a list!





First of all use a for loop, is more compact an readable, and do the same things:



for i in range(3):
champion = matchList['matches'][i]['champion']
print(champion)


Now use a list of champions:



champions = 
for i in range(3):
champion = matchList['matches'][i]['champion']
champions.append(champion)
print (champion)


In a more compact way:



champions = 
for i in range(3):
champions.append(matchList['matches'][i]['champion'])
print(champions)


now you can return it in a func:



    def getChamp(matchList):
champions =
for i in range(3):
champions.append(matchList['matches'][i]['champion'])
return champions


Maybe you would like to make the for loop more dynamic:



def getChamp(matchList):
champions =
for match in range(len(matchList['matches'])):
champions.append(matchList['matches'][match]['champion'])
return champions


This is a more pythonic way



def getChamp(matchList):
for match in matchList['matches']:
yield match['champion']
yield None


I hope this is what you needed to do!






share|improve this answer


























  • Can you please fix your syntax so that it is valid? Your later loops are invalid as well - you iterate over matches but still try to index with i.

    – MisterMiyagi
    Nov 25 '18 at 11:51











  • Now it's operative.

    – Lorenzo Fiamingo
    Nov 25 '18 at 12:20



















0














There are multiple ways to do but the following is simpler way using range and for loop. The data will be a list of your outputs. You may try yeild also



data=[matchList['matches'][i]['champion'] for i in range(3)]





share|improve this answer































    0














    Add all values ​to one variable and return it.



    def get_my_value():
    values =
    for i in range(3):
    champion = matchList['matches'][i]['champion']
    values.append(champion)
    return values

    data = get_my_value()





    share|improve this answer

































      0














      You can either collect all values and return them at once, or yield each value one after the other:



      # initial match list
      matchList = {'matches': [{'champion': champ} for champ in (101, 238, 157, None)]}

      def all_at_once():
      result =
      for match in matchList['matches']:
      result.append(match['champion'])
      return result

      def one_after_another():
      for match in matchList['matches']:
      yield match['champion']


      Both of these provide an iterable - you can use them in for loops, pass them to list or destructure them, for example:



      for item in one_after_another():
      print(item)

      print(*all_at_once())

      first, second, third, *rest = one_after_another()
      print(first, second, third)




      Since your transformation maps directly from one form to another, you can express both in comprehension form as well:



      all_at_once = [match['champion'] for match in matchList['matches']]
      one_after_another = (match['champion'] for match in matchList['matches'])




      While both provide iterables, the two are not equivalent. return means you build the entire list up front, whereas yield lazily computes each value.



      def print_all_at_once():
      result =
      for i in range(3):
      print(i)
      result.append(i)
      return result

      def print_one_after_another():
      for i in range(3):
      print(i)
      yield i

      # prints 0, 1, 2, 0, 1, 2
      for item in print_all_at_once():
      print(item)

      # print 0, 0, 1, 1, 2, 2
      for item in print_one_after_another():
      print(item)


      When you return a list, you can also reuse its content. In contrast, when you yield each value, it is gone after use:



      returned = print_all_at_once()  # already prints as list is built
      print('returned', *returned) # prints all values
      print('returned', *returned) # still prints all values

      yielded = print_one_after_another() # no print as nothing consumed yet
      print('yielded', *yielded) # prints all values and value generation
      print('yielded', *yielded) # prints no values





      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%2f53466867%2fhow-can-i-return-multiple-values-like-print-in-python-3%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        4 Answers
        4






        active

        oldest

        votes








        4 Answers
        4






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        0














        return can have only one value, (that can be an object like a list or something else)... why? Just because return is the value that assume the function. In the moment you do an assignment of a function for example



        def champs()
        return MJ KD LBJ

        champ = champs()


        In this way the number should be MJ, KD and LBJ at the same time... impossible conceptually. But we can return a list!





        First of all use a for loop, is more compact an readable, and do the same things:



        for i in range(3):
        champion = matchList['matches'][i]['champion']
        print(champion)


        Now use a list of champions:



        champions = 
        for i in range(3):
        champion = matchList['matches'][i]['champion']
        champions.append(champion)
        print (champion)


        In a more compact way:



        champions = 
        for i in range(3):
        champions.append(matchList['matches'][i]['champion'])
        print(champions)


        now you can return it in a func:



            def getChamp(matchList):
        champions =
        for i in range(3):
        champions.append(matchList['matches'][i]['champion'])
        return champions


        Maybe you would like to make the for loop more dynamic:



        def getChamp(matchList):
        champions =
        for match in range(len(matchList['matches'])):
        champions.append(matchList['matches'][match]['champion'])
        return champions


        This is a more pythonic way



        def getChamp(matchList):
        for match in matchList['matches']:
        yield match['champion']
        yield None


        I hope this is what you needed to do!






        share|improve this answer


























        • Can you please fix your syntax so that it is valid? Your later loops are invalid as well - you iterate over matches but still try to index with i.

          – MisterMiyagi
          Nov 25 '18 at 11:51











        • Now it's operative.

          – Lorenzo Fiamingo
          Nov 25 '18 at 12:20
















        0














        return can have only one value, (that can be an object like a list or something else)... why? Just because return is the value that assume the function. In the moment you do an assignment of a function for example



        def champs()
        return MJ KD LBJ

        champ = champs()


        In this way the number should be MJ, KD and LBJ at the same time... impossible conceptually. But we can return a list!





        First of all use a for loop, is more compact an readable, and do the same things:



        for i in range(3):
        champion = matchList['matches'][i]['champion']
        print(champion)


        Now use a list of champions:



        champions = 
        for i in range(3):
        champion = matchList['matches'][i]['champion']
        champions.append(champion)
        print (champion)


        In a more compact way:



        champions = 
        for i in range(3):
        champions.append(matchList['matches'][i]['champion'])
        print(champions)


        now you can return it in a func:



            def getChamp(matchList):
        champions =
        for i in range(3):
        champions.append(matchList['matches'][i]['champion'])
        return champions


        Maybe you would like to make the for loop more dynamic:



        def getChamp(matchList):
        champions =
        for match in range(len(matchList['matches'])):
        champions.append(matchList['matches'][match]['champion'])
        return champions


        This is a more pythonic way



        def getChamp(matchList):
        for match in matchList['matches']:
        yield match['champion']
        yield None


        I hope this is what you needed to do!






        share|improve this answer


























        • Can you please fix your syntax so that it is valid? Your later loops are invalid as well - you iterate over matches but still try to index with i.

          – MisterMiyagi
          Nov 25 '18 at 11:51











        • Now it's operative.

          – Lorenzo Fiamingo
          Nov 25 '18 at 12:20














        0












        0








        0







        return can have only one value, (that can be an object like a list or something else)... why? Just because return is the value that assume the function. In the moment you do an assignment of a function for example



        def champs()
        return MJ KD LBJ

        champ = champs()


        In this way the number should be MJ, KD and LBJ at the same time... impossible conceptually. But we can return a list!





        First of all use a for loop, is more compact an readable, and do the same things:



        for i in range(3):
        champion = matchList['matches'][i]['champion']
        print(champion)


        Now use a list of champions:



        champions = 
        for i in range(3):
        champion = matchList['matches'][i]['champion']
        champions.append(champion)
        print (champion)


        In a more compact way:



        champions = 
        for i in range(3):
        champions.append(matchList['matches'][i]['champion'])
        print(champions)


        now you can return it in a func:



            def getChamp(matchList):
        champions =
        for i in range(3):
        champions.append(matchList['matches'][i]['champion'])
        return champions


        Maybe you would like to make the for loop more dynamic:



        def getChamp(matchList):
        champions =
        for match in range(len(matchList['matches'])):
        champions.append(matchList['matches'][match]['champion'])
        return champions


        This is a more pythonic way



        def getChamp(matchList):
        for match in matchList['matches']:
        yield match['champion']
        yield None


        I hope this is what you needed to do!






        share|improve this answer















        return can have only one value, (that can be an object like a list or something else)... why? Just because return is the value that assume the function. In the moment you do an assignment of a function for example



        def champs()
        return MJ KD LBJ

        champ = champs()


        In this way the number should be MJ, KD and LBJ at the same time... impossible conceptually. But we can return a list!





        First of all use a for loop, is more compact an readable, and do the same things:



        for i in range(3):
        champion = matchList['matches'][i]['champion']
        print(champion)


        Now use a list of champions:



        champions = 
        for i in range(3):
        champion = matchList['matches'][i]['champion']
        champions.append(champion)
        print (champion)


        In a more compact way:



        champions = 
        for i in range(3):
        champions.append(matchList['matches'][i]['champion'])
        print(champions)


        now you can return it in a func:



            def getChamp(matchList):
        champions =
        for i in range(3):
        champions.append(matchList['matches'][i]['champion'])
        return champions


        Maybe you would like to make the for loop more dynamic:



        def getChamp(matchList):
        champions =
        for match in range(len(matchList['matches'])):
        champions.append(matchList['matches'][match]['champion'])
        return champions


        This is a more pythonic way



        def getChamp(matchList):
        for match in matchList['matches']:
        yield match['champion']
        yield None


        I hope this is what you needed to do!







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Dec 28 '18 at 23:28

























        answered Nov 25 '18 at 11:29









        Lorenzo FiamingoLorenzo Fiamingo

        849




        849













        • Can you please fix your syntax so that it is valid? Your later loops are invalid as well - you iterate over matches but still try to index with i.

          – MisterMiyagi
          Nov 25 '18 at 11:51











        • Now it's operative.

          – Lorenzo Fiamingo
          Nov 25 '18 at 12:20



















        • Can you please fix your syntax so that it is valid? Your later loops are invalid as well - you iterate over matches but still try to index with i.

          – MisterMiyagi
          Nov 25 '18 at 11:51











        • Now it's operative.

          – Lorenzo Fiamingo
          Nov 25 '18 at 12:20

















        Can you please fix your syntax so that it is valid? Your later loops are invalid as well - you iterate over matches but still try to index with i.

        – MisterMiyagi
        Nov 25 '18 at 11:51





        Can you please fix your syntax so that it is valid? Your later loops are invalid as well - you iterate over matches but still try to index with i.

        – MisterMiyagi
        Nov 25 '18 at 11:51













        Now it's operative.

        – Lorenzo Fiamingo
        Nov 25 '18 at 12:20





        Now it's operative.

        – Lorenzo Fiamingo
        Nov 25 '18 at 12:20













        0














        There are multiple ways to do but the following is simpler way using range and for loop. The data will be a list of your outputs. You may try yeild also



        data=[matchList['matches'][i]['champion'] for i in range(3)]





        share|improve this answer




























          0














          There are multiple ways to do but the following is simpler way using range and for loop. The data will be a list of your outputs. You may try yeild also



          data=[matchList['matches'][i]['champion'] for i in range(3)]





          share|improve this answer


























            0












            0








            0







            There are multiple ways to do but the following is simpler way using range and for loop. The data will be a list of your outputs. You may try yeild also



            data=[matchList['matches'][i]['champion'] for i in range(3)]





            share|improve this answer













            There are multiple ways to do but the following is simpler way using range and for loop. The data will be a list of your outputs. You may try yeild also



            data=[matchList['matches'][i]['champion'] for i in range(3)]






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 25 '18 at 11:19









            Pavan Kumar T SPavan Kumar T S

            565418




            565418























                0














                Add all values ​to one variable and return it.



                def get_my_value():
                values =
                for i in range(3):
                champion = matchList['matches'][i]['champion']
                values.append(champion)
                return values

                data = get_my_value()





                share|improve this answer






























                  0














                  Add all values ​to one variable and return it.



                  def get_my_value():
                  values =
                  for i in range(3):
                  champion = matchList['matches'][i]['champion']
                  values.append(champion)
                  return values

                  data = get_my_value()





                  share|improve this answer




























                    0












                    0








                    0







                    Add all values ​to one variable and return it.



                    def get_my_value():
                    values =
                    for i in range(3):
                    champion = matchList['matches'][i]['champion']
                    values.append(champion)
                    return values

                    data = get_my_value()





                    share|improve this answer















                    Add all values ​to one variable and return it.



                    def get_my_value():
                    values =
                    for i in range(3):
                    champion = matchList['matches'][i]['champion']
                    values.append(champion)
                    return values

                    data = get_my_value()






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Nov 25 '18 at 11:27

























                    answered Nov 25 '18 at 11:20







                    user7121223






























                        0














                        You can either collect all values and return them at once, or yield each value one after the other:



                        # initial match list
                        matchList = {'matches': [{'champion': champ} for champ in (101, 238, 157, None)]}

                        def all_at_once():
                        result =
                        for match in matchList['matches']:
                        result.append(match['champion'])
                        return result

                        def one_after_another():
                        for match in matchList['matches']:
                        yield match['champion']


                        Both of these provide an iterable - you can use them in for loops, pass them to list or destructure them, for example:



                        for item in one_after_another():
                        print(item)

                        print(*all_at_once())

                        first, second, third, *rest = one_after_another()
                        print(first, second, third)




                        Since your transformation maps directly from one form to another, you can express both in comprehension form as well:



                        all_at_once = [match['champion'] for match in matchList['matches']]
                        one_after_another = (match['champion'] for match in matchList['matches'])




                        While both provide iterables, the two are not equivalent. return means you build the entire list up front, whereas yield lazily computes each value.



                        def print_all_at_once():
                        result =
                        for i in range(3):
                        print(i)
                        result.append(i)
                        return result

                        def print_one_after_another():
                        for i in range(3):
                        print(i)
                        yield i

                        # prints 0, 1, 2, 0, 1, 2
                        for item in print_all_at_once():
                        print(item)

                        # print 0, 0, 1, 1, 2, 2
                        for item in print_one_after_another():
                        print(item)


                        When you return a list, you can also reuse its content. In contrast, when you yield each value, it is gone after use:



                        returned = print_all_at_once()  # already prints as list is built
                        print('returned', *returned) # prints all values
                        print('returned', *returned) # still prints all values

                        yielded = print_one_after_another() # no print as nothing consumed yet
                        print('yielded', *yielded) # prints all values and value generation
                        print('yielded', *yielded) # prints no values





                        share|improve this answer






























                          0














                          You can either collect all values and return them at once, or yield each value one after the other:



                          # initial match list
                          matchList = {'matches': [{'champion': champ} for champ in (101, 238, 157, None)]}

                          def all_at_once():
                          result =
                          for match in matchList['matches']:
                          result.append(match['champion'])
                          return result

                          def one_after_another():
                          for match in matchList['matches']:
                          yield match['champion']


                          Both of these provide an iterable - you can use them in for loops, pass them to list or destructure them, for example:



                          for item in one_after_another():
                          print(item)

                          print(*all_at_once())

                          first, second, third, *rest = one_after_another()
                          print(first, second, third)




                          Since your transformation maps directly from one form to another, you can express both in comprehension form as well:



                          all_at_once = [match['champion'] for match in matchList['matches']]
                          one_after_another = (match['champion'] for match in matchList['matches'])




                          While both provide iterables, the two are not equivalent. return means you build the entire list up front, whereas yield lazily computes each value.



                          def print_all_at_once():
                          result =
                          for i in range(3):
                          print(i)
                          result.append(i)
                          return result

                          def print_one_after_another():
                          for i in range(3):
                          print(i)
                          yield i

                          # prints 0, 1, 2, 0, 1, 2
                          for item in print_all_at_once():
                          print(item)

                          # print 0, 0, 1, 1, 2, 2
                          for item in print_one_after_another():
                          print(item)


                          When you return a list, you can also reuse its content. In contrast, when you yield each value, it is gone after use:



                          returned = print_all_at_once()  # already prints as list is built
                          print('returned', *returned) # prints all values
                          print('returned', *returned) # still prints all values

                          yielded = print_one_after_another() # no print as nothing consumed yet
                          print('yielded', *yielded) # prints all values and value generation
                          print('yielded', *yielded) # prints no values





                          share|improve this answer




























                            0












                            0








                            0







                            You can either collect all values and return them at once, or yield each value one after the other:



                            # initial match list
                            matchList = {'matches': [{'champion': champ} for champ in (101, 238, 157, None)]}

                            def all_at_once():
                            result =
                            for match in matchList['matches']:
                            result.append(match['champion'])
                            return result

                            def one_after_another():
                            for match in matchList['matches']:
                            yield match['champion']


                            Both of these provide an iterable - you can use them in for loops, pass them to list or destructure them, for example:



                            for item in one_after_another():
                            print(item)

                            print(*all_at_once())

                            first, second, third, *rest = one_after_another()
                            print(first, second, third)




                            Since your transformation maps directly from one form to another, you can express both in comprehension form as well:



                            all_at_once = [match['champion'] for match in matchList['matches']]
                            one_after_another = (match['champion'] for match in matchList['matches'])




                            While both provide iterables, the two are not equivalent. return means you build the entire list up front, whereas yield lazily computes each value.



                            def print_all_at_once():
                            result =
                            for i in range(3):
                            print(i)
                            result.append(i)
                            return result

                            def print_one_after_another():
                            for i in range(3):
                            print(i)
                            yield i

                            # prints 0, 1, 2, 0, 1, 2
                            for item in print_all_at_once():
                            print(item)

                            # print 0, 0, 1, 1, 2, 2
                            for item in print_one_after_another():
                            print(item)


                            When you return a list, you can also reuse its content. In contrast, when you yield each value, it is gone after use:



                            returned = print_all_at_once()  # already prints as list is built
                            print('returned', *returned) # prints all values
                            print('returned', *returned) # still prints all values

                            yielded = print_one_after_another() # no print as nothing consumed yet
                            print('yielded', *yielded) # prints all values and value generation
                            print('yielded', *yielded) # prints no values





                            share|improve this answer















                            You can either collect all values and return them at once, or yield each value one after the other:



                            # initial match list
                            matchList = {'matches': [{'champion': champ} for champ in (101, 238, 157, None)]}

                            def all_at_once():
                            result =
                            for match in matchList['matches']:
                            result.append(match['champion'])
                            return result

                            def one_after_another():
                            for match in matchList['matches']:
                            yield match['champion']


                            Both of these provide an iterable - you can use them in for loops, pass them to list or destructure them, for example:



                            for item in one_after_another():
                            print(item)

                            print(*all_at_once())

                            first, second, third, *rest = one_after_another()
                            print(first, second, third)




                            Since your transformation maps directly from one form to another, you can express both in comprehension form as well:



                            all_at_once = [match['champion'] for match in matchList['matches']]
                            one_after_another = (match['champion'] for match in matchList['matches'])




                            While both provide iterables, the two are not equivalent. return means you build the entire list up front, whereas yield lazily computes each value.



                            def print_all_at_once():
                            result =
                            for i in range(3):
                            print(i)
                            result.append(i)
                            return result

                            def print_one_after_another():
                            for i in range(3):
                            print(i)
                            yield i

                            # prints 0, 1, 2, 0, 1, 2
                            for item in print_all_at_once():
                            print(item)

                            # print 0, 0, 1, 1, 2, 2
                            for item in print_one_after_another():
                            print(item)


                            When you return a list, you can also reuse its content. In contrast, when you yield each value, it is gone after use:



                            returned = print_all_at_once()  # already prints as list is built
                            print('returned', *returned) # prints all values
                            print('returned', *returned) # still prints all values

                            yielded = print_one_after_another() # no print as nothing consumed yet
                            print('yielded', *yielded) # prints all values and value generation
                            print('yielded', *yielded) # prints no values






                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Nov 25 '18 at 11:44

























                            answered Nov 25 '18 at 11:19









                            MisterMiyagiMisterMiyagi

                            7,9022445




                            7,9022445






























                                draft saved

                                draft discarded




















































                                Thanks for contributing an answer to Stack Overflow!


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

                                But avoid



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

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


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




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53466867%2fhow-can-i-return-multiple-values-like-print-in-python-3%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

                                404 Error Contact Form 7 ajax form submitting

                                How to know if a Active Directory user can login interactively

                                TypeError: fit_transform() missing 1 required positional argument: 'X'