python what does yield do


To understand what yield does, you must understand what generators are. And before you can understand generators, you must understand iterables.
Iterables

When you create a list, you can read its items one by one. Reading its items one by one is called iteration:

>>> mylist = [1, 2, 3]
>>> for i in mylist:
...    print(i)
1
2
3

mylist is an iterable. When you use a list comprehension, you create a list, and so an iterable:

>>> mylist = [x*x for x in range(3)]
>>> for i in mylist:
...    print(i)
0
1
4

Everything you can use "for... in..." on is an iterable; lists, strings, files...

These iterables are handy because you can read them as much as you wish, but you store all the values in memory and this is not always what you want when you have a lot of values.
Generators

Generators are iterators, a kind of iterable you can only iterate over once. Generators do not store all the values in memory, they generate the values on the fly:

>>> mygenerator = (x*x for x in range(3))
>>> for i in mygenerator:
...    print(i)
0
1
4

It is just the same except you used () instead of []. BUT, you cannot perform for i in mygenerator a second time since generators can only be used once: they calculate 0, then forget about it and calculate 1, and end calculating 4, one by one.
Yield

yield is a keyword that is used like return, except the function will return a generator.

>>> def createGenerator():
...    mylist = range(3)
...    for i in mylist:
...        yield i*i
...
>>> mygenerator = createGenerator() # create a generator
>>> print(mygenerator) # mygenerator is an object!
<generator object createGenerator at 0xb7555c34>
>>> for i in mygenerator:
...     print(i)
0
1
4

Here it's a useless example, but it's handy when you know your function will return a huge set of values that you will only need to read once.

To master yield, you must understand that when you call the function, the code you have written in the function body does not run. The function only returns the generator object, this is a bit tricky :-)

Then, your code will continue from where it left off each time for uses the generator.

Now the hard part:

The first time the for calls the generator object created from your function, it will run the code in your function from the beginning until it hits yield, then it'll return the first value of the loop. Then, each subsequent call will run another iteration of the loop you have written in the function and return the next value. This will continue until the generator is considered empty, which happens when the function runs without hitting yield. That can be because the loop has come to an end, or because you no longer satisfy an "if/else".

Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
what yield means in python yield python examples python explain construct yield from &quot;yield from&quot; python yield python explained python function yield what's yield in python python what does yield return python function without yield pyothon yield how does the yield on pyhton works python code after yield yield vs yield from python get yield value python yield pyhton when to use yield python what is pyhthon yield python yield meaning get value from yield python yield pytohn purpose of a yield statement in python why yield? in python return or yield python yield in loop python what does yield does in python a = (yield) python return in yield python python yield x = can i have yield and return in python yield data python callback yield python what is the function of yield in python what is yield keyword in python what is yield used for in python? what yield do in python what does yield o in python yield ython python yield and return yield all python yield in pyhon python send to yield from python yield from and send yield from pytohn python3 with yield use yield in python class python yield callable python yield in function callable yield output python python yield() python return yield in function is yield python keyword yield pyython how does yield in python work yield pyth python yield method exam pytho yield example yield a method in python yield a function in python how is yield different from return in pyth pytho yield why use yield in python yield what is python what is the use of &quot;Yield&quot; in python meaning of yield keyword in Python when use yield python what yield in phython yield python for yield statement in python yield from () python yield py why do we use yield in python yield python command another way of yield pythn call yield function python use of yield in pythob python better to yield or return what th benifit of using yield in python when to use yield keyword in python python return value from yield how to use yield python yield pythno how to use yield function in python what yield does in python yield usage in python python when to use yield how does yield work in python how to use yield return in python python yield? what is the use of yield keyword in python python yield how to get func vakue yield in python how to get yield python python yield nedir simple yield in python yield example in python yield in python meaning what is yield in python 3 pythong do something after a yield pythin yield python 3 yield yield use in python yield [] python yield function in python example advantage of yield in python yield in pythn use yield with return in python use yield and return in python yield and return in python how to use yield in different way python using python yield yield in pyton yield python example yield from python documentation python requests and yield example python print a yield functions yield in python example python use yield what is yield python used for &quot;yield for&quot; python &quot;yield with&quot; python yield for python python yield with yield from lpython sre yield python how to use what happens in the back of python if yield in mentioned in a function python yield in function what does yield do ion python python using yield python yield documentation return yield python yield in python 3.7 yield pyton python yield for sre_yield python example python sre_yield how to import sre yield python 3 how to use sre yield python 3 python how yield funtion sre_yield python use of yield in python for in yield python define yield python yield en python python code after yield in a function yield py why simpy use yield how to print yield value in python yield return python what is python yield how to get yield value python yield from usage ptyhon python yield statement how does yield work python python how to use yield yield in python programiz use yield in pythnon python yield example what is the use of yield in python yield in python3 example of yield in python yield example python yield in python 3 python yield while why we use yield in python python yield function how to use yield in python python yield from yield in opython what does the yield functino do in python yield python documentation yield in py python yield get yield python3 from yield in python python3 yield yield statement python yield meaning in python python yield send python what is yield yield from in python yield from python python yield from example python return vs yield python what does input return what does the yield keyword do in python how to print yield what does yield do python yielfd python yield for loop yield vs return python yiel python what does yield mean python What does the &ldquo;yield&rdquo; keyword do? python return iterator with yield using yield in python yield iterator yield pythpn how to handle yield in python yield meaning python python yield lines after yield is executed can you stack yields python when to use yield in python The yield keyword is used to state that a function is a generator. what does yield do in python? yield x yield operator python how to yield from for loop calling yield in python without for yield function python python yield vs return python yield return yield command in python the yield function python python3 yield entry.name python byield using yield yield ***3 yield x**3 py yield what does yield func in python do what does yield in python do python 3 yeild from Explain how yield works python yielf yield pyhtong python yeld a function that yield returns what data type python yeald how does yield from in python work python yeild python access yielded objects python yeild command. yield next execution python for loop yield python yield and return python yield + python what does yield function do in python yield function in python yield python 3 what does yield mean in python yeild python what does yield do in python yeild in python what is yield function in python using yelid in a function what is yield in python what is yield python yeild pythoin yield keyword python yield return in python yield in python yield python what does exit(0) do in c++ what does append do in python python yield python what does yield do
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source