Python yield

Python yield keyword is similiar to the return keyword. It is used inside the function, for returning a generator instead of value.

>>> def f():
... x = [2,5,7]
... for i in x: yield 2 * i
...
>>> x = f()
>>> for i in x: print(i)
4
10
14
When the function f is first called, the generator is return instead of running the code inside. The code will be run when the for loop uses the generator. This is more efficient if the data size is large.