## ← 03-56 Generators

• 1 Follower
• 52 Lines

dummy description

### Get Embed Code x Embed video Use the following code to embed this video. See our usage guide for more details on embedding. Paste this in your document somewhere (closest to the closing body tag is preferable): ```<script type="text/javascript" src='https://amara.org/embedder-iframe'></script> ``` Paste this inside your HTML body, where you want to include the widget: ```<div class="amara-embed" data-url="http://www.youtube.com/watch?v=bqWCfnsxGqs" data-team="udacity"></div> ``` 3 Languages

Showing Revision 1 created 05/04/2012 by Amara Bot.

1. These list comprehensions are amazing.
2. They are one of my favorite parts of Python.
3. This is really a great way to program. It's very declarative.
4. You just say what you want, and the system makes it for you.
5. Unfortunately, they have a slight downside, which is that thus far
6. we've had to write out the starter list, and that's almost as much work
7. as just writing down what we need.
8. If only there were some way to generate this list,
9. especially if it's really big, without us having to write it down explicitly.
10. For example, suppose I started with a big list like this, 1, 2, 3, 4, 5, 6, 7.
11. I assert it's big. It's big for me.
12. This is heavy lifting for your professor.
13. And what I want to do is filter it down so that we only have the odd numbers.
14. I want to get this part out.
15. I'm going to show you a new way to do that in Python.
16. Here I've written a procedure called odds_only in Python
17. that takes a list of numbers, and it's going to iterate over them.
18. For every n in that list of numbers, if that number is odd,
19. we divide it by 2 and check the remainder.
20. 5 divided by 2 is 4 with remainder 1, so it's odd.
21. We yield that part into our results.
22. Note that I did not write the word "return."
23. Yield is a new special keyword that means we can use this sort of procedure
24. to gather up multiple results.
25. Let's imagine that this big list here was numbers.
26. We'll yield 1, not do anything with 2, yield 3,
27. not do anything with 4, yield 5, not do anything with 6, and yield 7,
28. and that's exactly the output that we wanted.
29. You can view this as sort of a convenient way of filtering.
30. Here I've written out our odds_only procedure.
31. I'm just going to show you in the interpreter how this plays out.
32. I'm using a list comprehension.
33. I want to print out every value of x that's in odds_only of 1, 2, 3, 4, 5,
34. and we get 1, 3, and 5, as we expected.
35. I'm also going to show you an even easier way to do this.
36. Snap, it'll be so cool.
37. Here I've written a list comprehension.
38. I want [x for x in [1, 2, 3, 4, 5].
39. But over here on the right I've put this sort of if conditional,
40. a guard or a predicate we might call it in mathematics,
41. and this is saying I only want you to yield
42. those numbers for which the predicate is true.
43. Only include x in the answer if x was an odd number,
44. and look, we get the answer we wanted.
45. I've written x a few times, but I can make these formulas
46. arbitrarily more complicated.
47. Here I've said take all of the odd numbers, and multiply them by 2.
48. Since 1, 3, and 5 were the odd numbers, 2, 6, and 10 are the multiplication by 2.
49. I love list comprehensions, and you will too soon.
50. A function like odds_only that uses yield
51. to potentially return multiple answers is called a generator
52. because you can use it to generate another list or another enumeration.