Write Pythonic Code Like a Seasoned Developer Transcripts
Chapter: Generators and Collections
Lecture: Counting generators

Login or purchase this course to watch this video and the rest of the course contents.
0:01 Now let's look at determining how many items are in a generator.
0:04 So if I have something like this, high measurements,
0:06 and it's I am getting the value, looping over some collection called measurements,
0:11 and I am doing a test, well, I actually have a really hard time
0:15 knowing how many are in there,
0:16 let's go look at that in PyCharm to see why.
0:19 So this is basically the same code that we used to talk
0:22 about generator expressions in the first place, this time,
0:25 this is set actually, fixed that, so here we have our high values,
0:29 now if I try to print out the "len" of them, get the length,
0:33 you can see PyCharm is already trying to tell me I am going down a bad path,
0:36 but let's see what happens anyway.
0:39 Boom, object or type generator has no length,
0:42 what might be wrong with getting the length,
0:44 well it could be infinite, and it might take forever to determine that,
0:47 but most importantly this is not going to work, all right?
0:50 So we are not going to do that because it crashes,
0:53 we could do something like this, we could say "create a list and pass into it
0:58 the high values in them", if you pass a collection here,
1:00 it will iterate overall the items and put it in the list and then I could print "len of list".
1:06 However, one of the beautiful things with generators is
1:09 if even if there is a million items here, we only hold one in memory ever,
1:13 well if we do this, we kind of undo that,
1:17 we now have one million of them in memory but let's just see that it works,
1:20 ta da, 4, OK, 4, that's cool,
1:25 so we can use a combination of some really cool things,
1:29 so if I had something like, let me just do something simple here,
1:32 I'll say "sum" and say given any set of numbers, I could add them up,
1:36 so those 3 numbers should have 7, we should see 7 down here, perfect, OK,
1:42 so we can use sum to give it some kind of collection here, let me separate these,
1:50 so we can kind of isolate them, count here and we'll say "count equals this", right,
1:55 so we could add this up using sum, we could leverage this,
1:58 along with another chained comprehension,
2:02 so what do I want to put in here,
2:04 for every item I see in here I would like to add one,
2:06 because what I want to know is how many items not what is some of the values,
2:10 so I could sum up the high values here, OK, now this actually broke,
2:16 now this is worth knowing why did this break, so we have 4 and 0,
2:20 obviously it should at least be 4 right, these are not 0 numbers that we saw,
2:24 but here is the thing, unlike lists, once you run through a generator,
2:28 it's done, you have to recreate it, so basically this,
2:32 the fact that I did this used up the generator in a sense,
2:36 you can only use them once, if you want to do it again you recreate it,
2:38 if that's not going to work for you, create a list comprehension instead.
2:42 There you go, so 375, there is not 375 items in there,
2:46 there is 4 so what I want to do is somehow in the best,
2:50 most efficient possible way go through this list and say
2:53 "for every time I find an item, give me one",
2:56 so what I can do is I can create another generator,
2:58 I can say "give me the number 1 for n in high values".
3:03 OK, so what we are going to do is we are going to through and say
3:05 "every time you find a value, I don't care, I am not going to use this value", let's say 1,
3:10 and that's going to add it up, now remember,
3:12 it's very Pythonic to say "if I am not going to use a value,
3:14 use this indicator say underscore",
3:17 like here is a variable that has to come out and be stored somewhere
3:21 but by saying underscore I have no any intention of using it,
3:24 so we should get the answer 4.
3:26 Beautiful, so here we have our generator, you saw generators don't have a length,
3:31 we can't use that, if we try to throw them into a list or something along those lines,
3:35 that's not good because that undoes all the benefits of the generator,
3:38 it loads everything in a memory all at once,
3:40 so we can use this cool combination of the sum method
3:44 and a chained generator expression,
3:47 so the chained generator expression is "give me one
3:50 for every time you find an item in high measurements",
3:52 which is itself a generator, and then if we sum up that set,
3:55 we actually get the number of items in the generator.
3:58 Remember, just be really careful it's used up after this,
4:02 so if you want to run it again, you need to either recreate it
4:06 by having the 5 lines above again,
4:08 or turn into a list where you can reuse it over and over.