Generators are memory efficient when dealing with large data set because they produce one item at a
time and only when they're asked for.
So the yield out data a single chunk at a time irrespective of the size of the input stream.
So we have just learned how to create generator objects using generator functions.
We can also create generator objects using generator expressions and simple generators can be easily
created on the fly using degenerated expressions.
And it makes building generators easy.
And a single line expression can be used to generate these values in a sequence rather than writing
a function for the same just like a lambda function create an anonymous function the generator expression
create an anonymous generator function.
Now let's see the syntax of a generated expression.
These syntax for a generated expression is similar to that of a list comprehension.
We know that list comprehension are used for creating new lists from other ITER bills but there is a
slight difference between the syntax for a list comprehension and a generated expression for a list
comprehension.
We use square brackets and within the square brackets we have an expression that is going to generate
a list from another item.
The expression here and means each item in this list is retrieved multiplied by two and added to the
new list list 1 now coming to the generated expression.
The square brackets in the list expression are replaced by parent.
This is like this also the generated expression that we have here is used to create a generated without
using the Hield key word that we have used in the generator function.
Another major difference between the list comprehension and generated expression is that a list comprehension
produces the entire list.
And Python Rizzo's memory for the whole list so the output of the list comprehension code is poured
8 twin and 16 and Python resolves memory for the entire list coming to the generated expression the
generator expression produces one item at a time and the the object that has been assigned to this generated
expression is a generator object and in order to compute the values in the generated object we need
the next function and parse the generator object and this is going to generate the first item in the
sequence which is Ford and calling the next function on this generator object or word and over again
generate the other values in this sequence.
So the generated expressions produce items only when they are asked for.
For this reason generated expressions are much more memory efficient than an equivalent list comprehension
here we have an expression for a list comprehension.
A list is going to be generated using this expression.
You're in square brackets.
So let's execute this and then display the output or list five.
So each element in this list has been multiplied by two and then added to the list.
List five now coming to the generator expression here.
The generator expression also multiplies each element in this list by two and gives the result.
It only computes the value of the result.
Only when we call the generated object using the next function.
So let's execute this line and then display the object.
June 5.
So it says a generator object has been assigned to this expression.
Now in order to compute the first value in the sequence we need to pass the generator object to the
next function.
So the first value has been generated.
The second value is 6 8 10.
And when there are no values to compute the next function has raised the stop iteration error just like
it does for generator functions so instead of writing Lindy functions in order to generate values in
a sequence.
A single line expression like this is used to generate the same values and with this we come to the
end of the topic I iterate those and generate us.