What Does "yield from" Do in Python?
Skills:
AI Pair Programming80%
Key Takeaways
The video explains the yield from statement in Python, its usage, and how it can be used to yield values from iterables such as generators, strings, and lists.
Full Transcript
what is going on guys welcome back in this video today we're going to learn what the yield from statement does in Python so let us get right into [Music] it all right so this is going to be a pretty short video today we're going to learn about the yield from statement in Python what it does and how to use it now for those of you who don't know what the yield keyword does in the first place it's a keyword used in generators to produce values to generate values or as the key says to yield values you can maybe compare it to a return statement but it's not quite the same because it produces values on demand here now let's look at a simple example let's say we have a function generator and this function has a couple of yield statements in it so yield one yield 2 and maybe something like yield hello world you can put any python object here basically now what we can do is we can create this generator we can say gen equals generator and then we can get the next value from the generator on demand so we can request the next value or we can also iterate over it and automatically request the next value so I can do something like print next gen and this is going to give me a one because that's the first yield statement here and if I do it three times I'm going to get one two and hello world if I do it another time I'm going to get uh a stop iteration exception there you go because there are no longer any values that are yielded and of course this can be be done in different ways we can do things like 4 I in range 10 you can yield I this also works then I'm going to get one two hell World 0 one 2 3 and so on I can also add conditions but that's the basic idea we have this generator function and the yield statements are uh producing values one after the other and this makes sense in a couple of use cases if you want to basically have the values on demand as I said now what does the yield from keyword do here what does it mean to go and say yield from and then something after it this is a python statement a python keyword combination that does something so what can we do here now what we can do here is we can basically pass any iterable so in a most simple way here I can just go ahead and say yield from 1 2 3 4 5 so let's delete all of this this is a possible thing in this case I would just yield the values from this collection so I would get these numbers here and I would yield them one after the other 1 2 3 4 5 but usually what you want to do is you want to have a generator and you want to have another generator function that yields values from a generator that we have defined before so I can have here uh def other generator and let's say up here I have I don't know yield one yield two yield three then in this other generator function here what I can do is I can yield my own values I can yield something like hello world and I can also yield something like goodbye and in between I might want to yield the values all the values from generator so what I can do here is I can say yield from generator and this is of course nice because an alternative way to write that if you don't use yield from is to say for Value in general generator yield value so that's basically the functionality here instead of having to write this explicitly as a loop instead of iterating actively over the generator we can write a simple short form we can say yield from generator and that has the same effect um in this case of course we need to say other generator then we're going to get Hello World 1 two 3 and if I add one more here we're going to also get goodbye that's the basic idea here now you need to consider that everything you pass here after a yield from statement will be treated as an iterable it will be iterated over which means that some things that are not necessarily lists can still be treated as iterables if you just yield hello world you're going to get hello world if you yield from Hello World hello world is considered an iterable which it is every string is an iterable so what we can do here is we can yield the individual letters so what happens here basically is you get h e l and so on and of course you don't have to do it like this you can also iterate over to the generator you can say for uh value in gen print value then you get all the values but this is how the string is yielded if you use a yield from because it's considered to be an iterable so yeah that's basically it this is just a short way of yielding from iterables this can be as I said a generator a string a list whatever anything that's iterable can be yielded with yield from so that's it for today's video I hope you enjoyed it and hope you learned something if so let me know by hitting a like button and leaving a comment in the comment section down below and of course don't forget to subscribe to this Channel and hit the notification Bell to not miss a single future video for free other than that thank you much for watching see you in the next video and bye
Original Description
In this video we take a look at the yield from statement in Python.
◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾
📚 Programming Books & Merch 📚
🐍 The Python Bible Book: https://www.neuralnine.com/books/
💻 The Algorithm Bible Book: https://www.neuralnine.com/books/
👕 Programming Merch: https://www.neuralnine.com/shop
💼 Services 💼
💻 Freelancing & Tutoring: https://www.neuralnine.com/services
🌐 Social Media & Contact 🌐
📱 Website: https://www.neuralnine.com/
📷 Instagram: https://www.instagram.com/neuralnine
🐦 Twitter: https://twitter.com/neuralnine
🤵 LinkedIn: https://www.linkedin.com/company/neuralnine/
📁 GitHub: https://github.com/NeuralNine
🎙 Discord: https://discord.gg/JU4xr8U3dm
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from NeuralNine · NeuralNine · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Visualizing Stock Data With Candlestick Charts in Python
NeuralNine
Python Beginner Tutorial #1 - Installation and First Program
NeuralNine
Python Beginner Tutorial #2 - Variables and Data Types
NeuralNine
Python Beginner Tutorial #3 - Operators and User Input
NeuralNine
Python Beginner Tutorial #4 - If Statements and Conditions
NeuralNine
Python Beginner Tutorial #5 - Loops
NeuralNine
Python Beginner Tutorial #6 - Sequences and Collections
NeuralNine
Python Beginner Tutorial #7 - Functions
NeuralNine
Python Beginner Tutorial #8 - Exception Handling
NeuralNine
Python Beginner Tutorial #9 - File Operations
NeuralNine
Python Beginner Tutorial #10 - String Functions
NeuralNine
Python Intermediate Tutorial #1 - Classes and Objects
NeuralNine
Python Intermediate Tutorial #2 - Inheritance
NeuralNine
Python Intermediate Tutorial #3 - Multithreading
NeuralNine
Python Intermediate Tutorial #4 - Synchronizing Threads
NeuralNine
Python Intermediate Tutorial #5 - Events and Daemon Threads
NeuralNine
Python Intermediate Tutorial #6 - Queues
NeuralNine
Python Intermediate Tutorial #7 - Sockets and Network Programming
NeuralNine
Python Intermediate Tutorial #8 - Database Programming
NeuralNine
Python Intermediate Tutorial #9 - Recursion
NeuralNine
Python Intermediate Tutorial #10 - XML Processing
NeuralNine
Python Intermediate Tutorial #11 - Logging
NeuralNine
Python Data Science Tutorial #1 - Anaconda and PyCharm Setup
NeuralNine
Python Data Science Tutorial #2 - NumPy Arrays
NeuralNine
Python Data Science Tutorial #3 - Numpy Functions
NeuralNine
Python Data Science Tutorial #4 - Plotting Functions With Matplotlib
NeuralNine
Python Data Science Tutorial #5 - Subplots and Multiple Windows
NeuralNine
Python Data Science Tutorial #6 - Matplotlib Styling
NeuralNine
Python Data Science Tutorial #7 - Bar Charts with Matplotlib
NeuralNine
Python Data Science Tutorial #8 - Pie Charts with Matplotlib
NeuralNine
Python Data Science Tutorial #9 - Plotting Histograms with Matplotlib
NeuralNine
Python Data Science Tutorial #10 - Scatter Plots with Matplotlib
NeuralNine
Python Data Science Tutorial #11 - 3D Plotting with Matplotlib
NeuralNine
Python Data Science Tutorial #12 - Pandas Series
NeuralNine
Python Data Science Tutorial #13 - Pandas Data Frames
NeuralNine
Python Data Science Tutorial #14 - Pandas Statistics
NeuralNine
Python Data Science Tutorial #15 - Pandas Sorting and Functions
NeuralNine
Python Data Science Tutorial #16 - Pandas Merging Data Frames
NeuralNine
Python Data Science Tutorial #17 - Pandas Queries
NeuralNine
Python Machine Learning Tutorial #1 - What is Machine Learning?
NeuralNine
Python Machine Learning Tutorial #2 - Linear Regression
NeuralNine
Python Machine Learning Tutorial #3 - K-Nearest Neighbors Classification
NeuralNine
Python Machine Learning #4 - Support Vector Machines
NeuralNine
Python Machine Learning Tutorial #5 - Decision Trees and Random Forest Classification
NeuralNine
Python Machine Learning Tutorial #6 - K-Means Clustering
NeuralNine
Python Machine Learning Tutorial #7 - Neural Networks
NeuralNine
Python Machine Learning Tutorial #8 - Handwritten Digit Recognition with Tensorflow
NeuralNine
Generating Poetic Texts with Recurrent Neural Networks in Python
NeuralNine
Stock Portfolio Visualization with Matplotlib in Python
NeuralNine
Analyzing Coronavirus with Python (COVID-19)
NeuralNine
Making Text Images Readable Again with Python and OpenCV
NeuralNine
Neural Networks Simply Explained (Theory)
NeuralNine
Motion Filtering with OpenCV in Python
NeuralNine
Top 5 Programming Languages To Learn in 2020
NeuralNine
Simple TCP Chat Room in Python
NeuralNine
Image Classification with Neural Networks in Python
NeuralNine
Edge Detection with OpenCV in Python
NeuralNine
S&P 500 Web Scraping with Python
NeuralNine
Simple Sentiment Text Analysis in Python
NeuralNine
Introduction - Algorithms & Data Structures #1
NeuralNine
More on: AI Pair Programming
View skill →
🎓
Tutor Explanation
DeepCamp AI