1 00:00:00,240 --> 00:00:09,240 Generators are memory efficient when dealing with large data set because they produce one item at a 2 00:00:09,240 --> 00:00:13,060 time and only when they're asked for. 3 00:00:13,500 --> 00:00:21,190 So the yield out data a single chunk at a time irrespective of the size of the input stream. 4 00:00:21,210 --> 00:00:28,680 So we have just learned how to create generator objects using generator functions. 5 00:00:28,680 --> 00:00:38,460 We can also create generator objects using generator expressions and simple generators can be easily 6 00:00:38,460 --> 00:00:42,400 created on the fly using degenerated expressions. 7 00:00:42,570 --> 00:00:45,750 And it makes building generators easy. 8 00:00:46,140 --> 00:00:53,370 And a single line expression can be used to generate these values in a sequence rather than writing 9 00:00:53,460 --> 00:01:04,860 a function for the same just like a lambda function create an anonymous function the generator expression 10 00:01:05,670 --> 00:01:10,450 create an anonymous generator function. 11 00:01:10,500 --> 00:01:15,510 Now let's see the syntax of a generated expression. 12 00:01:15,570 --> 00:01:21,450 These syntax for a generated expression is similar to that of a list comprehension. 13 00:01:23,710 --> 00:01:31,150 We know that list comprehension are used for creating new lists from other ITER bills but there is a 14 00:01:31,150 --> 00:01:38,260 slight difference between the syntax for a list comprehension and a generated expression for a list 15 00:01:38,260 --> 00:01:39,310 comprehension. 16 00:01:39,310 --> 00:01:48,310 We use square brackets and within the square brackets we have an expression that is going to generate 17 00:01:48,370 --> 00:01:51,530 a list from another item. 18 00:01:51,880 --> 00:02:01,210 The expression here and means each item in this list is retrieved multiplied by two and added to the 19 00:02:01,210 --> 00:02:07,820 new list list 1 now coming to the generated expression. 20 00:02:07,830 --> 00:02:12,930 The square brackets in the list expression are replaced by parent. 21 00:02:12,930 --> 00:02:24,650 This is like this also the generated expression that we have here is used to create a generated without 22 00:02:24,650 --> 00:02:29,990 using the Hield key word that we have used in the generator function. 23 00:02:29,990 --> 00:02:36,680 Another major difference between the list comprehension and generated expression is that a list comprehension 24 00:02:36,740 --> 00:02:39,040 produces the entire list. 25 00:02:39,140 --> 00:02:48,830 And Python Rizzo's memory for the whole list so the output of the list comprehension code is poured 26 00:02:49,580 --> 00:03:01,400 8 twin and 16 and Python resolves memory for the entire list coming to the generated expression the 27 00:03:01,490 --> 00:03:11,060 generator expression produces one item at a time and the the object that has been assigned to this generated 28 00:03:11,060 --> 00:03:21,500 expression is a generator object and in order to compute the values in the generated object we need 29 00:03:21,770 --> 00:03:32,360 the next function and parse the generator object and this is going to generate the first item in the 30 00:03:32,360 --> 00:03:40,780 sequence which is Ford and calling the next function on this generator object or word and over again 31 00:03:41,140 --> 00:03:44,880 generate the other values in this sequence. 32 00:03:45,130 --> 00:03:51,160 So the generated expressions produce items only when they are asked for. 33 00:03:51,190 --> 00:03:59,800 For this reason generated expressions are much more memory efficient than an equivalent list comprehension 34 00:04:01,090 --> 00:04:05,500 here we have an expression for a list comprehension. 35 00:04:05,500 --> 00:04:09,450 A list is going to be generated using this expression. 36 00:04:09,460 --> 00:04:11,260 You're in square brackets. 37 00:04:11,950 --> 00:04:18,750 So let's execute this and then display the output or list five. 38 00:04:20,470 --> 00:04:27,350 So each element in this list has been multiplied by two and then added to the list. 39 00:04:27,400 --> 00:04:32,210 List five now coming to the generator expression here. 40 00:04:32,320 --> 00:04:41,940 The generator expression also multiplies each element in this list by two and gives the result. 41 00:04:41,980 --> 00:04:44,950 It only computes the value of the result. 42 00:04:45,040 --> 00:04:51,160 Only when we call the generated object using the next function. 43 00:04:51,430 --> 00:04:58,560 So let's execute this line and then display the object. 44 00:04:58,580 --> 00:05:00,150 June 5. 45 00:05:01,190 --> 00:05:06,440 So it says a generator object has been assigned to this expression. 46 00:05:06,500 --> 00:05:17,300 Now in order to compute the first value in the sequence we need to pass the generator object to the 47 00:05:17,420 --> 00:05:19,990 next function. 48 00:05:20,320 --> 00:05:23,820 So the first value has been generated. 49 00:05:23,890 --> 00:05:28,810 The second value is 6 8 10. 50 00:05:29,380 --> 00:05:37,840 And when there are no values to compute the next function has raised the stop iteration error just like 51 00:05:37,840 --> 00:05:47,050 it does for generator functions so instead of writing Lindy functions in order to generate values in 52 00:05:47,050 --> 00:05:48,130 a sequence. 53 00:05:48,130 --> 00:05:58,350 A single line expression like this is used to generate the same values and with this we come to the 54 00:05:58,350 --> 00:06:02,910 end of the topic I iterate those and generate us.