Generator function and Comprehension in Python
What is 'Generator Function'?
Every function of python which has a keyword 'yield', written in it at least
one time is a generator function.
for example:
# xyz is Generator Function as it has yield written in it. def xyz(): x=1 print("1st yield") yield x x+=1 print("2nd yield") yield x x+=1 print("3rd yield") yield x #When we call xyz then it will return the reference of gnerator class a=xyz() print("Type of object in a=",type(a)) for i in a: print("value of i=",i) |
Output:
Type of object in a= <class 'generator'>
1st yield
value of i= 1
2nd yield
value of i= 2
3rd yield
value of i= 3
When we will call xyz function (Generator function) then it will return the
reference of generator type of object.
Now the question is, what is yield keyword?
So, the yield is a keyword written with a variable name. When the control
reaches the line where yield is written then yield returns a value of that
variable, which is written with yield.
How Generator Function works:
When we call a generator function then it returns the reference of
'generator' type of object. That generator type of object is iterable. The
proof of this is the above example, we can see that in the above example we
are iterating on 'a' which is holding generator type of object. In the
generator object returned by xyz, at every iteration, the yield function
will return a value of x and that value will be stored by 'i'. So this is
how generator function works. As we know that for an object to be iterable
there must be an __iter__() and a ___next__() function in it. So these
things are managed by the generator class.
Comprehension:
The below example which returns the reference of the generator, list, dict
is called comprehension. Comprehension means processing a sequence and
producing something new from it.
EXAMPLE-1
x=[10,20,30,40,50,60] #This is called comprehension b=(i for i in x) print("Type of value stored in b=",type(b)) c=list(b) print("value of c=",c) |
Output:
Type of value stored in b= <class 'generator'>
value of c= [10, 20, 30, 40, 50, 60]
The statement (i for i in x) will return the reference of the generator and the list function will
convert the generator back to the list.
EXAMPLE-2
x=[10,20,30,40,50,60] #This is clalled comprehension b=(i+2 for i in x) print("Type of value stored in b=",type(b)) c=list(b) print("value of c=",c) |
Output:
Type of value stored in b= <class 'generator'>
value of c= [12, 22, 32, 42, 52, 62]
The above example has incremented the value of each element of the list by
2.
EXAMPLE-3
This example will return a list in one line.
x=[10,20,30,40,50,60] b=[i for i in x if i%20==0] print("Type of value stored in b=",type(b)) print("value of b =",b) |
output:
Type of value stored in b= <class 'list'>
value of b = [20, 40, 60]
EXAMPLE-4
This shows how it works with tuples and sets
x={10,20,30,40,50,60} b=(i for i in x if i%20==0) print("Type of value stored in b=",type(b)) ans=set(b) print(ans) print("-"*40) x=(100,200,300,400,500,600) t=(i for i in x if i%200==0) print("Type of value stored in t=",type(t)) ans=tuple(t) print(ans) |
output:
Type of value stored in b= <class 'generator'>
{40, 20, 60}
----------------------------------------
Type of value stored in t= <class 'generator'>
(200, 400, 600)
EXAMPLE-5
In the below-given example, it is shown how to use comprehension with a dictionary.
x={1:"ABC", 2:"PQR", 3:"XYZ",4:"UVW"} b={i:x[i] for i in x if i%2==0} print(type(b)) print(b)
Output:
<class 'dict'>
{2: 'PQR', 4: 'UVW'}
Comments
Post a Comment