Python Tutorial: Generators – How to use them and the benefits you receive

///Python Tutorial: Generators – How to use them and the benefits you receive

Python Tutorial: Generators – How to use them and the benefits you receive

FavoriteLoadingAdd to favorites

Python Generators are often considered a somewhat advanced topic, but they are actually very easy to understand once you start using them on a regular basis. Actually, after you use generators for some time, you will often find them more readable and performant than other options.

In this video, we will look at what a python generator is, how and why we would use one, and the performance benefits they give us.

The code from this video can be found at:

✅ Support My Channel Through Patreon:

✅ Become a Channel Member:

✅ One-Time Contribution Through PayPal:

✅ Cryptocurrency Donations:
Bitcoin Wallet – 3MPH8oY2EAgbLVy7RBMinwcBntggi7qeG3
Ethereum Wallet – 0x151649418616068fB46C3598083817101d3bCD33
Litecoin Wallet – MPvEBY5fxGkmPQgocfJbxP6EmTo5UUXMot

✅ Corey’s Public Amazon Wishlist

✅ Equipment I Use and Books I Recommend:

▶️ You Can Find Me On:
My Website –
My Second Channel –
Facebook –
Twitter –
Instagram –

#Python

source

By |2019-07-01T20:18:46+00:00July 1st, 2019|Python Video Tutorials|24 Comments

24 Comments

  1. rosjio99 July 1, 2019 at 8:18 pm - Reply

    So if the performance gained by using generators is lost as soon as you convert it to a list or iterate over it, why even bother with generators?

  2. Tal Barak July 1, 2019 at 8:18 pm - Reply

    It seems that mem-profile doesn't exist anymore. If I'm not mistaken, the alternative today is to use memory-profiler which can be found here: https://pypi.org/project/memory-profiler/
    Please correct me if I'm wrong.

  3. Tushar Palande July 1, 2019 at 8:18 pm - Reply

    Thanks!

  4. Fire Alarm Apprentice July 1, 2019 at 8:18 pm - Reply

    its all python 2.x though, I was coding python3.6 and the examples in the video did not work and puzzled me very much until I ran python 2. Anyone know why 2 is still in use? Legacy code?

  5. Debasis Untouchable July 1, 2019 at 8:18 pm - Reply

    thanks for the video, just one thing in the 2nd example generator vs list 1000000 case, the generator takes considerable less time because you are not executing it, if it executes it will probably take the same time. So memory utilization is the only benefit

  6. j s July 1, 2019 at 8:18 pm - Reply

    Nicely done. thanks!

  7. Noobs da Estratégia July 1, 2019 at 8:18 pm - Reply

    Straight to the point, amazing!

  8. Jae Cordes July 1, 2019 at 8:18 pm - Reply

    The execution time is disingenuous. With the generator, you've just delayed the calculation to another segment of code. A true example of the performance metrics would show the utilization of the results as well.

  9. Ilan Aizelman July 1, 2019 at 8:18 pm - Reply

    GOLD.
    Thank you!

  10. sapan sagar pradhan July 1, 2019 at 8:18 pm - Reply

    Doesn't it store the 1st value in memory and wait for the next process? That's why the memory increased from 15.98 to 15.99MB. Please confirm. Thanks!

  11. Peter Osoo July 1, 2019 at 8:18 pm - Reply

    how comes you execute with print my_nums and not print (my_nums)

  12. Kevin Freeman July 1, 2019 at 8:18 pm - Reply

    @Corey: You present these in a clear, understandable fashion with plenty of proof to demonstrate the huge performance boost that generators make possible. This really helps these concepts to sink in, and helps me to understand how I can implement this concept into my own code. This is awesome, thanks!

  13. vignesh saravanan July 1, 2019 at 8:18 pm - Reply

    can i use the yield function with flask frameworks for a dictionary holding 10k items in it.

  14. Chirag Kalal July 1, 2019 at 8:18 pm - Reply

    It's an awesome explanation buddy. Thanks for making this kind of video. It is so helpful.

  15. sost July 1, 2019 at 8:18 pm - Reply

    Very useful. thank you

  16. python user July 1, 2019 at 8:18 pm - Reply

    @corey Schefer: Thanks for this video. It was simply great. I just wanted to see the code that you have written in mem_profile module. as I need to print that memory usage in my code as well.
    Thanks

  17. Calm Energy July 1, 2019 at 8:18 pm - Reply

    I love generators!

  18. Arnav SINGH July 1, 2019 at 8:18 pm - Reply

    I have watched a hell of tutorials but man, you, you are just awesome. Hats off to you.

  19. Justic H July 1, 2019 at 8:18 pm - Reply

    how cant you not charge for such infomatic???
    i hope people donate bitcoin to you

  20. Samrat Keshri July 1, 2019 at 8:18 pm - Reply

    good learning!!

  21. Why So Black July 1, 2019 at 8:18 pm - Reply

    Bro, you are the man. Every time I get stuck on a concept, I search for it and your videos pop up with with such a great explanation with a deep understanding. Thank you.

  22. ManiShankar Singh July 1, 2019 at 8:18 pm - Reply

    @Corey Schafer. Can you make a video on "yield from" syntax. That would be the real deal breaker. "yield" is easy to understand. I cannot bend my mind around "yield from".

  23. Traxxe s July 1, 2019 at 8:18 pm - Reply

    A generator is not being used to boost the performance of your program.
    The difference is that a regular loop first processes all the data and RETURNS ALL THE DATA AT ONCE. Imagine you have million pieces of data which takes 20 minutes to load! Thats a long long time before you can access any of the information and work with it.
    Now imagine having the same set of data but instead of a regular loop we use a generator. A generator DOES NOT RETURN ALL THE DATA AT ONCE. it returns one piece of the data which you can work with. And then it returns the next piece of the data and you can work with that.
    So instead of having to wait 20 Minutes to load everything before you can work with the data, you break down the data into smaller data pieces which you can work with as soon as you start the program.

  24. GTS- MEGHNI July 1, 2019 at 8:18 pm - Reply

    Thanks for sharing knowledge properly

Leave A Comment

*