Python Tutorial: Iterators and Iterables – What Are They and How Do They Work?
![*](https://i0.wp.com/allprowebdesigns.com/wp-content/uploads/2024/03/1710162729_maxresdefault.jpg?resize=840%2C430&ssl=1)
Video Title: Python Tutorial: Iterators and Iterables – What Are They and How Do They Work?
Hey there how’s it going everybody in this video I wanted to go over iterables and iterators now there’s a lot of confusion around these terms and exactly what they mean and I definitely understand the confusion because it’s also something that took me a while to understand so take a list for example so
A list is iterable but it is not an iterator so that can sound a little confusing but hopefully after you watch this video you’ll understand exactly what these terms mean and how to determine when something is iterable and when something is an iterator and we’re also going to learn how to make an
Object ourselves that is both an iterable and an iterator now this video isn’t only about understanding these definitions it’s also about understanding the concepts of iterators so that we can write better code so understanding these underlying concepts is going to help you write code that is more efficient and will also help you
See solutions to certain problems in a way that you might not have thought of before so with that said let’s go ahead and get started so to start off let’s first talk about intervals now what does it mean if something is iterable well on a higher level it simply means that it
Is something that can be looped over so for example a list is iterable because we can loop over a list now I know this might seem pretty basic right now but just stick with me and we’ll get to some lower level concepts in just a second
But for now let’s see what this looks like so I’m going to create a simple list and with a few items and then loop over it so I’ll say nums is equal to and we’ll just set this equal to one two three and now let’s loop over this so on
The next line here I’ll say for numb in nums and then we’ll just print out the number so if I run that then you can see that it loops through and we get the values one two three now loops aren’t the only things that we can loop over we can loop
Over tuples dictionaries strings files generators and all kinds of different objects so what’s actually going on in the background there how can we tell if something can be looped over or not or another way to ask this is how can we tell if something is iterable if
Something is iterable then it needs to have a special method called double underscore itter so let me write this out that is double underscore hitter now if you’ve seen my series on object-oriented programming in Python then you might know that these double underscore math kids are also called dunder methods or
Special methods or also magic methods so let’s see if our list has this dunder inter method so to look at the methods and attributes available to a specific object we can simply pass that into the built-in dir function and print that out so I’m going to comment out our for loop
For now and right below this I’m going to print dir and pass in our list of nums okay so this returns a good bit of stuff here let me make this a little larger so we can see all of these now this returns a lot of methods and
Attributes but if we look through here then we can see that one of these methods is double underscore itter so our list does have that dunder itter method so basically something can be looped over if it has that method so what the for loop is doing in the
Background is calling it err on our object and returning an iterator that we can loop over so that is why we call a list iterable okay so that brings us to iterators so I said that a list was iterable but it’s not an iterator but if
We run the dunder itter method on our list then it will return an iterator so what makes something an iterator so an iterator is an object with a state so that it remembers where it is during iteration now I know all of these terms might be running together but I think
This is all going to make sense when we see some examples so like I said an iterator has a state where it knows where it is during iteration and iterators also know how to get their next value they get their next value with a dunder next method so if I look
At my list methods here then you’ll see that there’s no dunder next method in for our list so our list doesn’t have a state and it doesn’t know how to get its next value so therefore it isn’t an iterator so for example if I try to
Print the next value of my list so if I go up here and just say print next nums and run that then this gives us an error and we can see that the error that we get says type error list object is not an iterator and by the way when I pass
An object into that next function like that in the background it’s actually trying to run the dunder next method on that object and we saw that our list doesn’t have a dunder next method so that’s why it’s telling us that our list object isn’t an iterator and it’s throwing an error there okay
But I did say that when we run our dunder itter method that it returns an iterator for us so let’s try that so let’s manually run that inner method on our list and see what the returned iterator looks like so above the for loop here I’m just going to say i
Underscore nums oops i underscore nums is equal to numbs dot double underscore hitter okay so now we have an iterator of our nums list now this is what the for loop gets for us in the background so let’s print this out and let’s also pass this into the dir function and see
What methods and attributes are available to our iterator so above here I’m just going to cut this out and right underneath here I’m going to print out dir of I numbs and I’m also just going to print this out by itself as well so let’s just print out I nums
And save that and run it okay so we got some output here but before we go over this let me change one thing here real quick so it’s a bit ugly to run our dunder methods directly like this so for most of these there are functions that
Call these in the background for us so just like I said that the next function calls the dunder next method the inner function calls the dunder inner method in the background so instead of doing it like we have here we can instead just say it er of nums and that will be the
Same thing so if we rerun this then we can see that we still get the same output but we don’t have to run that ugly dunder method okay so now let’s go over this so we can see that when we print it out our iterator that it tells
Us that this is a list iterator and there are lots of dunder methods returned here but the two that we want to focus on are dunder it ER and dunder next so we can see here that we have dunder it er method here and this also
Has a dunder next method as well now you might think it’s a little weird that this iterator has a dunder itter method because that’s what we ran on our list in order to get the iterator that we have now but it has to have a dunder inter method because iterators are also
Iterable but the difference is that this double underscore itter method returns the same object so it just returns self and we’ll see an example of this in just a minute okay so if we remember when I ran next on our original list it threw an error and said that our list wasn’t
An iterator and that’s because it didn’t have the dunder next method but our iterator that we just returned here does have a dunder next method so we should be able to get the next value from this so let me comment out these print statements here and underneath this let
Me print out will just print out next and we want to print out the next of the i underscore nums so if we run that then we can see that it prints out the next value of our list which at this point is just the first
Item in the original list now remember I said that an iterator is an object with a state so that it remembers where it is during iteration so if we run next on this again then it should remember where it left off and print the next value so
Let’s go ahead and try that so I’m going to copy and paste this a couple of times so we have three print statements here now and let me run this so we can see that each time we printed the next value it remembered where it left off and the
Next method pointed to the next value in the iterator okay so right now we’ve printed all of the items in our original list the original list only has one two and three so what happens if we print one more time so let me just paste in
Another one down here and save this and run it so we can see that when we ran that one more time we got an error so it threw a stop iteration exception now that’s expected so if we run out of values then it raises a stop iteration exception but that’s what iterators are
Meant to do so when we hit a stop iteration exception it means that the iterator has been exhausted and has no more values so we can see it when we run through them manually like this but when we run a normal for loop it knows how to handle the stop iteration exception and
Doesn’t show it to us so in the background a for loop is basically doing something like this it’s first getting an iterator of our original object like we’ve done here on line four and then it’s getting the next values until it hits a stop iteration exception so basically it looks
Something like this so let me remove all of these here basically it’s doing something like while true and then inside here try to get a next item so we’ll say item equals next i underscore nums and then we’ll just print that item for now now we also need to check for an
Exception so we’ll say accept stop iteration and if we get a stop iteration then we will simply break out of the while loop so now if I run this code then we can see that we basically got the same result from before when we ran our simple for loop now another
Characteristic of iterators that I should have mentioned is that they can only go forward so there’s no going backwards resetting it or making a copy of it you can only go forward by calling next if you need to start from scratch then you can simply create a new
Iterator object from scratch and just start that over okay so why does any of this really matter so what are the practical examples of knowing this well one example is that we can add these methods to our own classes and make them iterable as well so let’s see an example
So let’s create a class that basically behaves like the built-in range function so I’m just going to overwrite everything here and I will close out of the output and now we’re going to create a class that basically behaves like the built-in range function so I’ll say
Class my range okay so at this point I should mention that we’re going to get into some object-oriented concepts by creating classes and I do have a series on object-oriented programming and Python if any of you are interested now I cover a lot in that series that I’m
Not going to go into detail about here such as the anit method and instance attributes and things like that so if this part is confusing to you in any way then you can watch that series to get a better understanding of what we’re doing here okay so if we’re going to make our
Own range class that behaves like the built-in range then we need a starting point and an end point so in and nip method so I’ll say def double underscore and knit so first we want to pass in self which will be the instance and then start and then in so that we
Have a start point and an end point and within the anit method I’ll say self dot value is equal to the start and self dot end is equal to the end okay so now we want to make this class iterable now remember for something to be iterable
And needs to have a dunder itter method so let’s go ahead and add that so we will add a method here of double underscore I ter and that will take in self okay so at this point if we remember our inter method has to return
An iterator and by that I mean that it has to return an object that has a dunder next method but we can simply create a dunder next method within this class itself and if we do that then we can simply return this same object from our inter method so we can just simply
Say return self now this object that it’s returning here needs to have a dunder next method so since its returning itself it means that we need to add our dunder next method to this class so let’s go ahead and do that so we’ll say def double underscore next and
We will take in self okay and at this point we need to figure out the logic in order for the iterator to step through the values now if you remember I said that iterators have a state and remember where they left off in the iteration and
If they’re out of values then it raises a stop iteration exception so first let’s put in our check to see if there are any more values because we don’t want to do anything if we’re already at the end so let’s go ahead and say if self dot value is greater than or equal
To self dot end then we will simply raise a stop iteration okay so if our value isn’t greater than or equal to the in value then we can just continue on so now we can say that the current you is equal to self dot value and then
We want to increment self dot value so that we are actually looping through something so I’ll say self dot value plus equals one and now we can just return that current value that we first grabbed okay so each time we run this next method it will come in and get the
Current value increment the old value by one and then return current so now this should work as an iterator so to use this let’s just go ahead and create one of these objects so I’ll say nums is equal to my range and we will pass in a
Start value of one and an N value of ten so now let me scroll down here a little bit now let’s loop through this so I’ll say for numb in numbs print out none so if I save that and run it then we can see that it looped through the values one
Through ten okay so this class that we created is iterable because we can use it in a for loop but it’s also an iterator because it has a dunder next method so we should be able to call next on it manually to get the values one at
A time so instead of our for loop let me just overwrite this and instead let’s just print out next of nums so if I save that and run it then you can see that we just get the first value and let me do this a few more times here and save that
And run it and you can see that it loops through the next values now if we were to continue on printing those out then eventually we’d hit the stop iteration exception okay so we’re just about finished up with this video but there’s one more thing that I want to show you
About iterators so some of you may have used generators now they’re extremely useful for creating easy-to-read iterators they look a lot like normal functions but instead of returning a result they instead yield a value and when a when they yield a value it keeps that state until the generator is run
Again and yields the next value so generators are iterators as well but the dunder it ER and dunder next methods are created automatically so we don’t have to create them like we did in our class so with that said let me write a generator function that does
The exact same thing that our range class does so to do this we can simply come under the class here and we can create a generator function by saying def and I’ll just call this my underscore range and again we want a start and an end argument now this isn’t
A class so we don’t need self here because we’re not going to be working with specific instances so all we need our start and end parameters okay and at the beginning here we can just say current is equal to the start value that we pass in and now we’ll just say while
Current is less than end then we want to yield the current value and then after we yield then we’ll say current plus equals one and save that okay so now let’s use this generator instead of our class and see if we get the same result and then I’ll walk through this one more
Time just to point out exactly what’s going on here so if I replace that and run it then you can see that we get the same result okay so let’s think about what’s going on here so when we create this it comes into this function here
With a start value of one and an end value of 10 and then it sets current equal to start which means that current would be equal to one and then we’re saying while the current is less than end then we want to yield the current value so the first time through it’ll
Say while one is less than ten which it is so it’ll come in here and yield one and then it will increment that by one and now we’re still in this while loop so now current will be equal to two since it increment it by one and it says
Okay is two less than ten yes so yield the next value and then increment by one is 3 less than ten yes so yield that and it will keep doing that until it breaks out of this while loop and whenever it breaks out of that while loop then it
Hits that stop iteration exception so just like with our class we should be able to loop through this all the way using a for loop as well so if I say for numb in nums and then print none if I save that and run it then you
Can see that it prints out the values 1 through 10 so this generator function works just like the class does that we created but I just think it’s much more readable than the class so I like to use these generator functions whenever I can okay now the last thing that I want to
Say about iterators is that they don’t actually need to end so one of the cool things about iterators is that they can simply go on forever so as long as there is a next value then our iterator will keep getting each next value one at a time so for example if I
Were to remove our end variable here and then just put in a true statement for our while loop so let me do that so I’ll just remove this end parameter and then instead of putting in a conditional here I’m just going to say while true and now
This generator no longer accepts an end value so we can just pass in a start value so I’m just going to remove the tin and say that we want to start at one so now our generator iterator is going to simply go on forever so since I’m looping through this with a
For loop it’s actually going to get stuck in an infinite loop and it’s going to start printing from our start value counting up by one forever so I’ll show you what this looks like but I wouldn’t recommend you do this on your machine because it’s hard to stop it from
Executing and can freeze up your computer but I’m gonna try to run it and stop it quickly so let me do this so I’m just going to quickly run this and then stop it okay so I was able to run that and then stop it by hitting ctrl C on my
Machine but even in that short time it already counted up by a lot so it counted up almost to a million values there and that would have continued going indefinitely if I hadn’t stopped it now like I said the iterator can go on forever but it still only fetches one
Value at a time now this really comes in handy when writing memory efficient programs because sometimes there are so many values that you just couldn’t hold them all in memory if you were to put them in a list or a tuple but if you simply use an iterator then you can loop
Over one value at a time until it’s exhausted or just let it keep going so for example let’s say that you were writing a password cracker and wanted to brute-force it by checking all of the possible password using a certain group of characters well there would be so many different
Possible passwords that you couldn’t possibly hold them all in a single list your computer would just run out of memory but you could use an iterator to loop through all those possibilities one at a time and it might take some time until you find a result but your program
Wouldn’t take up all of your computer’s memory and it wouldn’t crash so maybe we’ll write a program that cracks easy passwords in the near future so that you can see how something like that would work okay so I think that we’re just about ready to end the video but let’s
Do a quick recap of everything that we learned since some of the stuff can kind of run together so what does it mean for something to be iterable so broadly speaking it means that it can be looped over but more specifically it means that the object needs to return an iterator
Object from its dunder itter method and the iterator that is returned from dunder itter must define a dunder next method which accesses elements in the container one at a time so just because something is iterable doesn’t mean that it is an iterator and again what does it mean for
Something to be an iterator so that means that it’s an object with a state so that it remembers where it’s at during its iteration and it knows how to fetch its next value using the dunder next method and when it doesn’t have an x value then it raises a stop iteration
Exception now I’m going to try something a little different with this video so I’ve put together a coding problem to practice what we’ve learned in this video and I’m going to release the problem and solution in a separate video shortly after this video is released and
I’ve had requests for a long time to come up with some problems where viewers could practice what they’ve learned so I’m going to give that a shot and see if you all think that it’s beneficial now the reason that I’m gonna release the coding problem in a separate video is
Because I want to keep the lessons and problem separate so that the lessons don’t get too long also if anyone wants to skip the problems altogether then they can just simply skip the coding problem videos instead of trying to fast-forward through parts of the lesson so I’ll leave a link to the
Coding problem for this video in the description section below if anyone would like to practice what we’ve learned here but with that said I think that’s going to do it for this video hopefully now you have a clear understanding of what it means for something to be iterable and what it
Means for something to be a knitter now we also learned how to add this functionality to our own classes as well as writing generators with this functionality as well now that we’ve covered iterators one of my next videos that I release is going to be on the
Inner tools module and how that module provides some awesome tools for working with iterators but if anyone has any questions about what we covered in this video then feel free to ask in the comment section below and I’ll do my best to answer those and if you enjoyed
These tutorials and would like to support them then there are several ways you can do that the easiest ways to simply like the video and give it a thumbs up and also it’s a huge help to share these videos with anyone who you think would find them useful and if you
Have the means you can contribute through patreon and there’s a link to that page in the description section below be sure to subscribe for future videos and thank you all for watching you
-
Sale!
Wireless WIFI Repeater Extender Amplifier Booster 300Mbps
$29.99$14.99 Add to cartWireless WIFI Repeater Extender Amplifier Booster 300Mbps
Categories: Electronics, Wi-Fi Router, Wireless Wi-Fi Extender Tags: 300Mbps, 802.11N, Amplifier, Booster, Extender, mobile wi-fi booster, Remote, WIFI, Wireless, Wireless WIFI, Wireless WIFI Repeater, Wireless WIFI Repeater Extender, Wireless WIFI Repeater Extender Amplifier, Wireless WIFI Repeater Extender Amplifier Booster, Wireless WIFI Repeater Extender Amplifier Booster 300Mbps$29.99$14.99 -
Sale!
Full RGB Light Design Gaming Headset Headphones with Mic
$24.99$14.99 Add to cartFull RGB Light Design Gaming Headset Headphones with Mic
Categories: Electronics, Gaming, Gaming Headsets Tags: Design, Full, Full RGB Light Design Gaming Headset, Full RGB Light Design Gaming Headset Headphones, Full RGB Light Design Gaming Headset Headphones with Mic, Gamer, Gaming, Gaming Headset Headphones, gaming headset wireless, Headphone, Headphones, Headset, Light, Mic, Package, RGB$24.99$14.99 -
Sale!
Wireless BlueTooth Multi-Device Keyboard Mouse Combo
$39.99$19.99 Add to cartWireless BlueTooth Multi-Device Keyboard Mouse Combo
Categories: Electronics, Gaming, Gaming Keyboards, Keyboard Mouse Combos Tags: Combo, Keyboard, keyboard mouse combos, Mouse, MultiDevice, Set, WireKeyboard Mouse Combo, Wireless, Wireless BlueTooth Keyboard Mouse Combo, Wireless BlueTooth Keyboard Mouse Combos, Wireless BlueTooth Multi-Device Keyboard Mouse Combo, Wireless BlueTooth Multi-Device Keyboard Mouse Combos$39.99$19.99 -
Sale!
High Back Leather Executive Adjustable Swivel Gaming Chair with Headrest and Lumbar
$199.99$139.99 Add to cartHigh Back Leather Executive Adjustable Swivel Gaming Chair with Headrest and Lumbar
Categories: Gaming, Gaming Chairs Tags: Adjustable, Chair, computer chairs, Desk, Executive, Gaming, Girl, Headrest, High, High Back Leather Executive Adjustable Swivel Gaming Chair, High Back Leather Executive Adjustable Swivel Gaming Chair with Headrest, High Back Leather Executive Adjustable Swivel Gaming Chair with Headrest and Lumbar, High Back Leather Executive Adjustable Swivel Gaming Chairs, Leather, Lumbar, Office, Racing, Swivel$199.99$139.99 -
Sale!
Professional LED Light Wired Gaming Headphones with Noise Cancelling Microphone
$29.99$19.99 Select optionsProfessional LED Light Wired Gaming Headphones with Noise Cancelling Microphone
SKU: N/A Categories: Electronics, Gaming, Gaming Headsets Tags: Cancelling, Gaming, Gaming Headphones with Noise Cancelling Microphone, gaming headset, Headphones, Headset, LED, Light, Mic, Microphone, Noise, Professional, Professional LED Light Wired Gaming Headphones, Professional LED Light Wired Gaming Headphones with Noise Cancelling Microphone, Wired, Wired Gaming Headphones, Wired Gaming Headphones with Noise Cancelling Microphone$29.99$19.99 -
Sale!
Gaming Desk with LED Lights USB Power Outlets and Charging Ports
$349.99$249.99 Select optionsGaming Desk with LED Lights USB Power Outlets and Charging Ports
SKU: N/A Categories: Computer Desk, Gaming, Gaming Desk Tags: and Charging Ports, Charging, Desk, Desks, Gaming, gaming desk with led lights, Gaming Desks with LED Lights, Home, LED, Lights, Monitor, Office, Outlets, Port, Power, Room, Stand, USB, USB Power Outlets, White, Workstation$349.99$249.99 -
Sale!
Wired Mixed Backlit Anti-Ghosting Gaming Keyboard
$99.99$79.99 Add to cartWired Mixed Backlit Anti-Ghosting Gaming Keyboard
Categories: Electronics, Gaming, Gaming Keyboards Tags: Antighosting, Backlit, Blue, brown, Gaming, Gaming Keyboard, gaming keyboards, gaming keyboards and mouse, Keyboard, Laptop, Switch, Wired, Wired Mixed Backlit Anti-Ghosting Gaming Keyboard, Wired Mixed Backlit Anti-Ghosting Gaming Keyboards, Wired Mixed Backlit Gaming Keyboard$99.99$79.99 -
Sale!
Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset
$119.99$59.99 Add to cartWireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset
Categories: Electronics, Gaming, Gaming Headsets Tags: 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset, ANC, Audio, Bluetooth, Cancellation, Ear, Earphone, gaming headset, Headphones, Headset, Hi-Res Over the Ear Headphones Headset, HiRes, Noise, Wireless, Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Headphones, Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset, Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headsets$119.99$59.99 -
Sale!
Wired Sports Gaming Headset Earbuds with Microphone
$19.99$9.99 Select optionsWired Sports Gaming Headset Earbuds with Microphone
SKU: N/A Categories: Gaming, Gaming Headsets Tags: Accessories, Earbud, Earphone, Earphones, Gaming, gaming headset with microphone, Headphones, Headset, IOS, Microphone, Sports, Wired, Wired Sports Gaming Headset Earbuds, Wired Sports Gaming Headset Earbuds with Microphone, Wired Sports Headset Earbuds$19.99$9.99 -
Sale!
150W Universal Multi USB Fast Charger 16 Port MAX Charging Station
$49.99$29.99 Add to cart150W Universal Multi USB Fast Charger 16 Port MAX Charging Station
Categories: Charging Stations, Electronics Tags: 150W, 150W Charging Station, 150W Universal Multi USB Charging Station, 150W Universal Multi USB Fast Charger 16 Port MAX Charging Station, 150W Universal Multi USB Fast Charger 16 Port MAX Charging Stations, 150W Universal Multi USB MAX Charging Station, 16 Port MAX Charging Station, 3.5A, Charger, Charging, Fast, laptop charging stations, Max, Multi, Port, Stand, Station, Universal, USB$49.99$29.99
thank you very clear your explanation
why did you define _iter_ function inside the class MyRange@13:23??
isn't the _iter_ function a defined magic method already defined in Python??
thank you so much for this video. I've been queering bing chat and bard chat for a couple of hours on the iterable and iterator topics but none of them managed to explain the stuff to me in a so clear way that you did.
Thank you , you’re the goat
Even after years, This is easy to understand and relevant, Thank you very much man
Thanks for the awesome video.
How do you make your terminal clutter free like that?
You are amazing.. 🙂
Wow really good explanations! This makes a lot of sense
when i add that code in iDLE it comes back with Traceback (most recent call last):
File "C:/Users/lesli/AppData/Local/Programs/Python/Python311/aaa.py", line 10, in <module>
print(num)
NameError: name 'num' is not defined. Did you mean: 'nums'? any particular reason why?
One of the best explanations on iterators. Thanks a ton!
Simply the best!
Nice video
Just out of curiosity, how does an iterable object which isnt a iterator work(since it wont be having the next method in its class definition work).
Excellent explanations are always!!
It really sums up the concepts in a concise but precise way.
Thank you a lot for this!
Dunder Mifflin
Great explanation
Is it fair to say that "Iterators are to Python programs what paging and virtual memory is for OS".
1. In Python, an object can be called an "iterable" if it has the dunder method _iter_ which should return an iterator and an iterator is an object that has the method __next__.
2. _iter_ is a method that a python list has.
3. By performing the for loop on the list, python invokes that list's _iter_ method which returns an iterator that we can loop over.
we can do next(mylist._iter__()) which invokes the iterator's __next_ method or call or invoke it directly: mylist.__iter__().__next__()
we can also do next(iter(mylist))
"Generators are iterators as well, but the duder iter and duner next methods are created automatically."
Thank you so much Corey, much appreciated how helpful this(these) videos are, just thank you 👍
_mifflin_
2022