title
Python Tutorial: Iterators and Iterables - What Are They and How Do They Work?
description
In this Python Programming Tutorial, we will be learning about iterators and iterables. There is a lot of confusion around these terms and exactly what they mean. We're also going to learn how to make an object ourselves that is both an iterable and an iterator. This video isn't only about understanding these definitions. It's also about understanding the concepts of iterators so that you can write better code. 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 you might not have thought of before. So with that said, let's get started...
The code from this video can be found at:
https://github.com/CoreyMSchafer/code_snippets/tree/master/Python/Iterators
Coding Problem for This Video:
https://youtu.be/C3Z9lJXI6Qw
Object-Oriented Series:
https://www.youtube.com/playlist?list=PL-osiE80TeTsqhIuOqKhwlXsIBIdSeYtc
✅ Support My Channel Through Patreon:
https://www.patreon.com/coreyms
✅ Become a Channel Member:
https://www.youtube.com/channel/UCCezIgC97PvUuR4_gbFUs5g/join
✅ One-Time Contribution Through PayPal:
https://goo.gl/649HFY
✅ Cryptocurrency Donations:
Bitcoin Wallet - 3MPH8oY2EAgbLVy7RBMinwcBntggi7qeG3
Ethereum Wallet - 0x151649418616068fB46C3598083817101d3bCD33
Litecoin Wallet - MPvEBY5fxGkmPQgocfJbxP6EmTo5UUXMot
✅ Corey's Public Amazon Wishlist
http://a.co/inIyro1
✅ Equipment I Use and Books I Recommend:
https://www.amazon.com/shop/coreyschafer
▶️ You Can Find Me On:
My Website - http://coreyms.com/
My Second Channel - https://www.youtube.com/c/coreymschafer
Facebook - https://www.facebook.com/CoreyMSchafer
Twitter - https://twitter.com/CoreyMSchafer
Instagram - https://www.instagram.com/coreymschafer/
#Python
detail
{'title': 'Python Tutorial: Iterators and Iterables - What Are They and How Do They Work?', 'heatmap': [{'end': 222.193, 'start': 166.699, 'weight': 0.761}, {'end': 735.958, 'start': 701.941, 'weight': 0.792}, {'end': 876.853, 'start': 842.655, 'weight': 0.747}, {'end': 937.57, 'start': 901.596, 'weight': 0.789}], 'summary': 'This tutorial series comprehensively covers the concepts of iterables and iterators in python, providing practical examples and custom class creation to enhance understanding and write more efficient code.', 'chapters': [{'end': 72.944, 'segs': [{'end': 52.668, 'src': 'embed', 'start': 18.116, 'weight': 0, 'content': [{'end': 21.237, 'text': 'So that can sound a little confusing, but hopefully after you watch this video,', 'start': 18.116, 'duration': 3.121}, {'end': 27.359, 'text': "you'll understand exactly what these terms mean and how to determine when something is iterable and when something is an iterator.", 'start': 21.237, 'duration': 6.122}, {'end': 32.521, 'text': "And we're also going to learn how to make an object ourselves that is both an iterable and an iterator.", 'start': 27.699, 'duration': 4.822}, {'end': 36.082, 'text': "Now, this video isn't only about understanding these definitions.", 'start': 32.921, 'duration': 3.161}, {'end': 40.723, 'text': "it's also about understanding the concepts of iterators so that we can write better code.", 'start': 36.082, 'duration': 4.641}, {'end': 50.286, 'text': '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.', 'start': 41.083, 'duration': 9.203}, {'end': 52.668, 'text': "So with that said, let's go ahead and get started.", 'start': 50.866, 'duration': 1.802}], 'summary': 'Learn about iterables, iterators, and efficient code writing.', 'duration': 34.552, 'max_score': 18.116, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/jTYiNjvnHZY/pics/jTYiNjvnHZY18116.jpg'}], 'start': 0.229, 'title': 'Understanding iterables and iterators', 'summary': 'Discusses the concepts of iterables and iterators, explaining that an iterable is something that can be looped over, like a list, and aims to help viewers understand these terms and write more efficient code.', 'chapters': [{'end': 72.944, 'start': 0.229, 'title': 'Understanding iterables and iterators', 'summary': 'Discusses the concepts of iterables and iterators, explaining that an iterable is something that can be looped over, like a list, and aims to help viewers understand these terms and write more efficient code.', 'duration': 72.715, 'highlights': ['The video explains that an iterable is something that can be looped over, such as a list, helping viewers understand the concept and write better code.', 'Understanding these underlying concepts will help write more efficient code and see solutions to certain problems in a new way.']}], 'duration': 72.715, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/jTYiNjvnHZY/pics/jTYiNjvnHZY229.jpg', 'highlights': ['Understanding these underlying concepts will help write more efficient code and see solutions to certain problems in a new way.', 'The video explains that an iterable is something that can be looped over, such as a list, helping viewers understand the concept and write better code.']}, {'end': 727.394, 'segs': [{'end': 131.473, 'src': 'embed', 'start': 93.801, 'weight': 3, 'content': [{'end': 98.164, 'text': 'So if I run that, then you can see that it loops through and we get the values 1, 2, 3.', 'start': 93.801, 'duration': 4.363}, {'end': 100.347, 'text': "Now loops aren't the only things that we can loop over.", 'start': 98.164, 'duration': 2.183}, {'end': 107.557, 'text': 'We can loop over tuples, dictionaries, strings, files, generators, and all kinds of different objects.', 'start': 100.688, 'duration': 6.869}, {'end': 110.241, 'text': "So what's actually going on in the background there?", 'start': 107.958, 'duration': 2.283}, {'end': 113.546, 'text': 'How can we tell if something can be looped over or not?', 'start': 110.982, 'duration': 2.564}, {'end': 117.327, 'text': 'Or another way to ask this is how can we tell if something is iterable?', 'start': 114.046, 'duration': 3.281}, {'end': 124.23, 'text': 'If something is iterable, then it needs to have a special method called double underscore iter.', 'start': 118.008, 'duration': 6.222}, {'end': 125.751, 'text': 'So let me write this out.', 'start': 124.45, 'duration': 1.301}, {'end': 127.531, 'text': 'That is double underscore iter.', 'start': 125.871, 'duration': 1.66}, {'end': 131.473, 'text': "Now, if you've seen my series on object-oriented programming in Python,", 'start': 127.972, 'duration': 3.501}], 'summary': 'Python can loop over tuples, dictionaries, strings, files, generators, and other objects; iterability requires a method named double underscore iter.', 'duration': 37.672, 'max_score': 93.801, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/jTYiNjvnHZY/pics/jTYiNjvnHZY93801.jpg'}, {'end': 225.715, 'src': 'heatmap', 'start': 155.249, 'weight': 5, 'content': [{'end': 161.414, 'text': "And right below this, I'm going to print dir and pass in our list of nums.", 'start': 155.249, 'duration': 6.165}, {'end': 163.717, 'text': 'Okay, so this returns a good bit of stuff here.', 'start': 161.875, 'duration': 1.842}, {'end': 166.179, 'text': 'Let me make this a little larger so we can see all of these.', 'start': 163.757, 'duration': 2.422}, {'end': 173.824, 'text': '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 iter.', 'start': 166.699, 'duration': 7.125}, {'end': 176.446, 'text': 'So our list does have that dunder iter method.', 'start': 174.145, 'duration': 2.301}, {'end': 180.209, 'text': 'So basically something can be looped over if it has that method.', 'start': 176.827, 'duration': 3.382}, {'end': 187.615, 'text': 'So what the for loop is doing in the background is calling iter on our object and returning an iterator that we can loop over.', 'start': 180.529, 'duration': 7.086}, {'end': 190.377, 'text': 'So that is why we call a list iterable.', 'start': 187.995, 'duration': 2.382}, {'end': 192.858, 'text': 'Okay, so that brings us to iterators.', 'start': 191.057, 'duration': 1.801}, {'end': 196.84, 'text': "So I said that a list was iterable, but it's not an iterator.", 'start': 193.198, 'duration': 3.642}, {'end': 202.383, 'text': 'But if we run the dunder iter method on our list, then it will return an iterator.', 'start': 197.3, 'duration': 5.083}, {'end': 210.727, 'text': 'So what makes something an iterator? So an iterator is an object with a state so that it remembers where it is during iteration.', 'start': 202.623, 'duration': 8.104}, {'end': 216.75, 'text': '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.', 'start': 211.147, 'duration': 5.603}, {'end': 222.193, 'text': 'So like I said, an iterator has a state where it knows where it is during iteration.', 'start': 217.13, 'duration': 5.063}, {'end': 225.715, 'text': 'And iterators also know how to get their next value.', 'start': 222.733, 'duration': 2.982}], 'summary': 'A list is iterable but not an iterator; calling dunder iter on a list returns an iterator with a state for iteration.', 'duration': 70.466, 'max_score': 155.249, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/jTYiNjvnHZY/pics/jTYiNjvnHZY155249.jpg'}, {'end': 225.715, 'src': 'embed', 'start': 197.3, 'weight': 1, 'content': [{'end': 202.383, 'text': 'But if we run the dunder iter method on our list, then it will return an iterator.', 'start': 197.3, 'duration': 5.083}, {'end': 210.727, 'text': 'So what makes something an iterator? So an iterator is an object with a state so that it remembers where it is during iteration.', 'start': 202.623, 'duration': 8.104}, {'end': 216.75, 'text': '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.', 'start': 211.147, 'duration': 5.603}, {'end': 222.193, 'text': 'So like I said, an iterator has a state where it knows where it is during iteration.', 'start': 217.13, 'duration': 5.063}, {'end': 225.715, 'text': 'And iterators also know how to get their next value.', 'start': 222.733, 'duration': 2.982}], 'summary': 'Dunder iter method returns an iterator with a state for iteration and ability to get next value.', 'duration': 28.415, 'max_score': 197.3, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/jTYiNjvnHZY/pics/jTYiNjvnHZY197300.jpg'}, {'end': 485.675, 'src': 'embed', 'start': 457.074, 'weight': 2, 'content': [{'end': 463.539, 'text': 'Now remember, I said that an iterator is an object with a state so that it remembers where it is during iteration.', 'start': 457.074, 'duration': 6.465}, {'end': 469.383, 'text': 'So if we run next on this again, then it should remember where it left off and print the next value.', 'start': 463.939, 'duration': 5.444}, {'end': 471.024, 'text': "So let's go ahead and try that.", 'start': 469.583, 'duration': 1.441}, {'end': 473.826, 'text': "So I'm going to copy and paste this a couple of times.", 'start': 471.364, 'duration': 2.462}, {'end': 476.048, 'text': 'So we have three print statements here now.', 'start': 474.186, 'duration': 1.862}, {'end': 477.889, 'text': 'And let me run this.', 'start': 476.628, 'duration': 1.261}, {'end': 485.675, 'text': '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.', 'start': 478.289, 'duration': 7.386}], 'summary': "An iterator is an object with a state, remembering where it left off, printing the next value. demonstrated by running 'next' method multiple times.", 'duration': 28.601, 'max_score': 457.074, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/jTYiNjvnHZY/pics/jTYiNjvnHZY457074.jpg'}, {'end': 620.144, 'src': 'embed', 'start': 589.935, 'weight': 0, 'content': [{'end': 593.876, 'text': "So there's no going backwards, resetting it, or making a copy of it.", 'start': 589.935, 'duration': 3.941}, {'end': 596.197, 'text': 'You can only go forward by calling next.', 'start': 594.196, 'duration': 2.001}, {'end': 600.938, 'text': 'If you need to start from scratch, then you can simply create a new iterator object from scratch.', 'start': 596.577, 'duration': 4.361}, {'end': 602.558, 'text': 'and just start that over.', 'start': 601.378, 'duration': 1.18}, {'end': 605.099, 'text': 'Okay, so why does any of this really matter?', 'start': 602.939, 'duration': 2.16}, {'end': 607.6, 'text': 'So what are the practical examples of knowing this??', 'start': 605.219, 'duration': 2.381}, {'end': 613.362, 'text': 'Well, one example is that we can add these methods to our own classes and make them iterable as well.', 'start': 607.98, 'duration': 5.382}, {'end': 615.082, 'text': "So let's see an example.", 'start': 613.782, 'duration': 1.3}, {'end': 620.144, 'text': "So let's create a class that basically behaves like the built-in range function.", 'start': 615.522, 'duration': 4.622}], 'summary': 'Iterators allow forward movement, enabling creation of iterable classes.', 'duration': 30.209, 'max_score': 589.935, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/jTYiNjvnHZY/pics/jTYiNjvnHZY589935.jpg'}], 'start': 73.465, 'title': 'Python iteration and iterability', 'summary': 'Covers python looping through lists, checking iterability, and using dunder methods. it also explains iterators, iterables, and practical examples of creating custom iterable classes, with a focus on for loops and dunder methods.', 'chapters': [{'end': 154.508, 'start': 73.465, 'title': 'Python looping and iterable objects', 'summary': 'Introduces looping through lists, checking iterability, and using dunder methods in python, showcasing an example of looping through a list of numbers 1, 2, 3 and explaining the concept of iterability through dunder methods.', 'duration': 81.043, 'highlights': ['The chapter demonstrates looping through a list of numbers 1, 2, 3 and printing them, showcasing the basic concept of iteration in Python.', 'It explains the concept of iterability in Python and the requirement for objects to have a special method called double underscore iter to be iterable.', 'The chapter introduces the concept of dunder methods or special methods in Python, mentioning that these methods are also called magic methods.']}, {'end': 727.394, 'start': 155.249, 'title': 'Understanding iterators and iterables', 'summary': 'Explains the concepts of iterators and iterables by demonstrating how an object can be iterable if it has a dunder iter method and how an iterator is an object with a state that knows how to get its next value using a dunder next method, highlighting practical examples of creating custom iterable classes and how for loops work in the background.', 'duration': 572.145, 'highlights': ['An iterator is an object with a state so that it remembers where it is during iteration, and it knows how to get its next value with a dunder next method.', 'A for loop calls iter on the object and returns an iterator that can be looped over, handling the stop iteration exception in the background.', 'Creating custom iterable classes and adding dunder iter and dunder next methods allows objects to be iterable and behave like built-in functions such as range.']}], 'duration': 653.929, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/jTYiNjvnHZY/pics/jTYiNjvnHZY73465.jpg', 'highlights': ['Creating custom iterable classes and adding dunder iter and dunder next methods allows objects to be iterable and behave like built-in functions such as range', 'An iterator is an object with a state so that it remembers where it is during iteration, and it knows how to get its next value with a dunder next method', 'The chapter demonstrates looping through a list of numbers 1, 2, 3 and printing them, showcasing the basic concept of iteration in Python', 'It explains the concept of iterability in Python and the requirement for objects to have a special method called double underscore iter to be iterable', 'The chapter introduces the concept of dunder methods or special methods in Python, mentioning that these methods are also called magic methods', 'A for loop calls iter on the object and returns an iterator that can be looped over, handling the stop iteration exception in the background']}, {'end': 1054.856, 'segs': [{'end': 754.659, 'src': 'embed', 'start': 727.834, 'weight': 1, 'content': [{'end': 732.856, 'text': 'And if we do that, then we can simply return this same object from our iter method.', 'start': 727.834, 'duration': 5.022}, {'end': 735.958, 'text': 'So we can just simply say return self.', 'start': 733.197, 'duration': 2.761}, {'end': 740.903, 'text': "Now this object that it's returning here needs to have a dunder next method.", 'start': 736.838, 'duration': 4.065}, {'end': 747.03, 'text': "So since it's returning itself, it means that we need to add our dunder next method to this class.", 'start': 741.384, 'duration': 5.646}, {'end': 749.073, 'text': "So let's go ahead and do that.", 'start': 747.691, 'duration': 1.382}, {'end': 754.659, 'text': "So we'll say def double underscore next and we will take in self.", 'start': 749.513, 'duration': 5.146}], 'summary': 'The class needs to have a dunder next method added to it.', 'duration': 26.825, 'max_score': 727.834, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/jTYiNjvnHZY/pics/jTYiNjvnHZY727834.jpg'}, {'end': 895.128, 'src': 'heatmap', 'start': 842.655, 'weight': 0, 'content': [{'end': 848.259, 'text': 'And we will pass in a start value of one and an end value of 10.', 'start': 842.655, 'duration': 5.604}, {'end': 850.16, 'text': 'So now let me scroll down here a little bit.', 'start': 848.259, 'duration': 1.901}, {'end': 852.142, 'text': "Now let's loop through this.", 'start': 850.68, 'duration': 1.462}, {'end': 856.985, 'text': "So I'll say for num in nums, print out num.", 'start': 852.302, 'duration': 4.683}, {'end': 863.447, 'text': 'so if I save that and run it, then we can see that it looped through the values 1 through 10.', 'start': 858.584, 'duration': 4.863}, {'end': 872.111, 'text': "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.", 'start': 863.447, 'duration': 8.664}, {'end': 876.853, 'text': 'so we should be able to call next on it manually to get the values one at a time.', 'start': 872.111, 'duration': 4.742}, {'end': 884.357, 'text': "so instead of our for loop, let me just overwrite this and instead let's just print out next of nums.", 'start': 876.853, 'duration': 7.504}, {'end': 888.04, 'text': 'so if I save that and run it, then you can see that we just get the first value.', 'start': 884.357, 'duration': 3.683}, {'end': 892.025, 'text': 'And let me do this a few more times here and save that and run it.', 'start': 888.341, 'duration': 3.684}, {'end': 895.128, 'text': 'And you can see that it loops through the next values.', 'start': 892.465, 'duration': 2.663}], 'summary': 'A class is created to iterate through values 1 to 10, demonstrating iterable and iterator capabilities.', 'duration': 74.17, 'max_score': 842.655, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/jTYiNjvnHZY/pics/jTYiNjvnHZY842655.jpg'}, {'end': 937.57, 'src': 'heatmap', 'start': 901.596, 'weight': 0.789, 'content': [{'end': 906.841, 'text': "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.", 'start': 901.596, 'duration': 5.245}, {'end': 909.584, 'text': 'So some of you may have used generators.', 'start': 907.222, 'duration': 2.362}, {'end': 913.288, 'text': "Now they're extremely useful for creating easy to read iterators.", 'start': 909.904, 'duration': 3.384}, {'end': 919.575, 'text': 'They look a lot like normal functions, but instead of returning a result, they instead yield a value.', 'start': 913.648, 'duration': 5.927}, {'end': 927.481, 'text': 'And when they yield a value, it keeps that state until the generator is run again and yields the next value.', 'start': 920.055, 'duration': 7.426}, {'end': 934.867, 'text': 'So generators are iterators as well, but the dunder iter and dunder next methods are created automatically.', 'start': 927.902, 'duration': 6.965}, {'end': 937.57, 'text': "So we don't have to create them like we did in our class.", 'start': 934.928, 'duration': 2.642}], 'summary': 'Generators are useful for creating easy-to-read iterators, yielding values and keeping state until the next value.', 'duration': 35.974, 'max_score': 901.596, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/jTYiNjvnHZY/pics/jTYiNjvnHZY901596.jpg'}], 'start': 727.834, 'title': 'Creating and understanding python iterators', 'summary': 'Explains creating iterable and iterator in python with a custom class, allowing iteration through values 1 to 10, and discusses manual iteration of iterators, generators, and creation of a generator function.', 'chapters': [{'end': 872.111, 'start': 727.834, 'title': 'Creating an iterable and iterator', 'summary': 'Explains the process of creating an iterable and iterator in python, utilizing a custom class with a dunder next method, allowing for the iteration through values 1 to 10 and highlighting the importance of having a state and managing the iteration logic.', 'duration': 144.277, 'highlights': ['The class created is both iterable and an iterator due to the inclusion of a dunder next method, enabling it to be used in a for loop and facilitating iteration through the values 1 to 10.', 'Emphasizing the significance of iterators having a state and remembering where they left off in the iteration, accompanied by the necessity for a proper iteration logic to avoid raising a stop iteration exception.', "Detailing the logic for stepping through the values within the iterator, ensuring it doesn't exceed the end value, incrementing the value during iteration, and returning the current value while explaining the iterative process.", 'Explaining the process of returning the same object from the iter method and the addition of a dunder next method to the class for facilitating the iteration through values 1 to 10 and ensuring it is iterable and an iterator.']}, {'end': 1054.856, 'start': 872.111, 'title': 'Understanding iterators and generators', 'summary': 'Discusses the manual iteration of iterators, the concept of generators, and the creation of a generator function to mimic the behavior of a class, demonstrating the process of yielding values and automatic generation of dunder iter and dunder next methods.', 'duration': 182.745, 'highlights': ["The process of manually iterating through values using the 'next' method is demonstrated, showcasing the ability to obtain values one at a time and the occurrence of the stop iteration exception.", 'Generators are introduced as useful for creating easy-to-read iterators, functioning similarly to normal functions but yielding values instead of returning results.', "The creation of a generator function, 'my_range', is detailed, highlighting the usage of 'yield' to produce values and the automatic creation of dunder iter and dunder next methods for generators."]}], 'duration': 327.022, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/jTYiNjvnHZY/pics/jTYiNjvnHZY727834.jpg', 'highlights': ['The class created is both iterable and an iterator, enabling iteration through values 1 to 10.', 'Emphasizing the significance of iterators having a state and proper iteration logic.', 'Generators are introduced as useful for creating easy-to-read iterators.', "The process of manually iterating through values using the 'next' method is demonstrated.", 'Detailing the logic for stepping through the values within the iterator.']}, {'end': 1375.076, 'segs': [{'end': 1119.684, 'src': 'embed', 'start': 1094.367, 'weight': 1, 'content': [{'end': 1100.89, 'text': 'so as long as there is a next value, then our iterator will keep getting each next value one at a time.', 'start': 1094.367, 'duration': 6.523}, {'end': 1108.335, 'text': '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.', 'start': 1100.89, 'duration': 7.445}, {'end': 1116.521, 'text': "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.", 'start': 1108.335, 'duration': 8.186}, {'end': 1119.684, 'text': 'And now this generator no longer accepts an end value.', 'start': 1116.862, 'duration': 2.822}], 'summary': 'The iterator will keep getting each next value one at a time as long as there is a next value.', 'duration': 25.317, 'max_score': 1094.367, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/jTYiNjvnHZY/pics/jTYiNjvnHZY1094367.jpg'}, {'end': 1164.158, 'src': 'embed', 'start': 1135.717, 'weight': 0, 'content': [{'end': 1140.44, 'text': "And it's going to start printing from our start value, counting up by one forever.", 'start': 1135.717, 'duration': 4.723}, {'end': 1144.642, 'text': "So I'll show you what this looks like, but I wouldn't recommend you do this on your machine,", 'start': 1140.82, 'duration': 3.822}, {'end': 1148.225, 'text': "because it's hard to stop it from executing and can freeze up your computer.", 'start': 1144.642, 'duration': 3.583}, {'end': 1150.687, 'text': "But I'm going to try to run it and stop it quickly.", 'start': 1148.585, 'duration': 2.102}, {'end': 1152.208, 'text': 'So let me do this.', 'start': 1151.407, 'duration': 0.801}, {'end': 1155.81, 'text': "So I'm just going to quickly run this and then stop it.", 'start': 1152.348, 'duration': 3.462}, {'end': 1160.895, 'text': 'Okay, so I was able to run that and then stop it by hitting control C on my machine.', 'start': 1156.611, 'duration': 4.284}, {'end': 1164.158, 'text': 'But even in that short time, it already counted up by a lot.', 'start': 1161.295, 'duration': 2.863}], 'summary': 'Program counts up by a lot in a short time', 'duration': 28.441, 'max_score': 1135.717, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/jTYiNjvnHZY/pics/jTYiNjvnHZY1135717.jpg'}, {'end': 1223.665, 'src': 'embed', 'start': 1195.457, 'weight': 2, 'content': [{'end': 1198.258, 'text': "So for example, let's say that you are writing a password cracker.", 'start': 1195.457, 'duration': 2.801}, {'end': 1203.705, 'text': 'and wanted to brute force it by checking all of the possible passwords using a certain group of characters.', 'start': 1198.718, 'duration': 4.987}, {'end': 1209.492, 'text': "Well, there would be so many different possible passwords that you couldn't possibly hold them all in a single list.", 'start': 1204.165, 'duration': 5.327}, {'end': 1211.835, 'text': 'Your computer would just run out of memory.', 'start': 1210.033, 'duration': 1.802}, {'end': 1215.861, 'text': 'But you could use an iterator to loop through all those possibilities one at a time.', 'start': 1212.196, 'duration': 3.665}, {'end': 1223.665, 'text': "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.", 'start': 1216.261, 'duration': 7.404}], 'summary': 'Using an iterator can efficiently handle large password-cracking tasks and prevent memory overload.', 'duration': 28.208, 'max_score': 1195.457, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/jTYiNjvnHZY/pics/jTYiNjvnHZY1195457.jpg'}, {'end': 1294.374, 'src': 'embed', 'start': 1267.489, 'weight': 5, 'content': [{'end': 1277.84, 'text': "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.", 'start': 1267.489, 'duration': 10.351}, {'end': 1282.225, 'text': "and when it doesn't have an x value, then it raises a stop iteration exception.", 'start': 1277.84, 'duration': 4.385}, {'end': 1285.208, 'text': "now i'm going to try something a little different with this video.", 'start': 1282.225, 'duration': 2.983}, {'end': 1294.374, 'text': "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.", 'start': 1285.208, 'duration': 9.166}], 'summary': 'Python iterator object remembers state and fetches next value with dunder next method.', 'duration': 26.885, 'max_score': 1267.489, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/jTYiNjvnHZY/pics/jTYiNjvnHZY1267489.jpg'}, {'end': 1344.622, 'src': 'embed', 'start': 1319.883, 'weight': 3, 'content': [{'end': 1326.686, 'text': "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.", 'start': 1319.883, 'duration': 6.803}, {'end': 1329.347, 'text': "But with that said, I think that's going to do it for this video.", 'start': 1327.046, 'duration': 2.301}, {'end': 1335.67, 'text': 'Hopefully now you have a clear understanding of what it means for something to be iterable and what it means for something to be an iterator.', 'start': 1329.828, 'duration': 5.842}, {'end': 1342.459, 'text': 'Now we also learned how to add this functionality to our own classes as well as writing generators with this functionality as well.', 'start': 1336.17, 'duration': 6.289}, {'end': 1344.622, 'text': "Now that we've covered iterators,", 'start': 1343.14, 'duration': 1.482}], 'summary': 'Learned about iterables, iterators, and generator functions for classes.', 'duration': 24.739, 'max_score': 1319.883, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/jTYiNjvnHZY/pics/jTYiNjvnHZY1319883.jpg'}, {'end': 1375.076, 'src': 'embed', 'start': 1352.552, 'weight': 4, 'content': [{'end': 1356.897, 'text': 'But if anyone has any questions about what we covered in this video, then feel free to ask in the comment section below,', 'start': 1352.552, 'duration': 4.345}, {'end': 1358.018, 'text': "and I'll do my best to answer those.", 'start': 1356.897, 'duration': 1.121}, {'end': 1361.942, 'text': 'And if you enjoy these tutorials and would like to support them, then there are several ways you can do that.', 'start': 1358.378, 'duration': 3.564}, {'end': 1364.485, 'text': 'The easiest way is to simply like the video and give it a thumbs up.', 'start': 1362.282, 'duration': 2.203}, {'end': 1367.968, 'text': "And also it's a huge help to share these videos with anyone who you think would find them useful.", 'start': 1364.825, 'duration': 3.143}, {'end': 1372.273, 'text': "And if you have the means, you can contribute through Patreon and there's a link to that page in the description section below.", 'start': 1368.349, 'duration': 3.924}, {'end': 1375.076, 'text': 'Be sure to subscribe for future videos and thank you all for watching.', 'start': 1372.713, 'duration': 2.363}], 'summary': 'Encourages viewers to support by liking, sharing, and contributing through patreon.', 'duration': 22.524, 'max_score': 1352.552, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/jTYiNjvnHZY/pics/jTYiNjvnHZY1352552.jpg'}], 'start': 1054.856, 'title': 'Iterators and iteration', 'summary': 'Covers iterator functions, iterable and iterator objects, and announces a coding problem to practice the concepts, with an invitation to engage and a hint about the upcoming tutorial on the iter tools module.', 'chapters': [{'end': 1267.129, 'start': 1054.856, 'title': 'Iterator functions and infinite looping', 'summary': 'Discusses the concept of iterator functions, including how to create and loop through a generator function, and the advantage of iterators in writing memory-efficient programs such as password crackers. the discussion also covers the definition of iterable and iterator objects.', 'duration': 212.273, 'highlights': ['Iterating through a generator function using a for loop prints values 1 through 10', 'Creating an iterator that runs indefinitely and counting up almost to a million values', 'Explaining the advantage of iterators in writing memory-efficient programs like password crackers']}, {'end': 1375.076, 'start': 1267.489, 'title': 'Understanding iterators and coding problems', 'summary': 'Covers the concept of iterators, including the functionality of iterable and iterator objects, and announces the release of a separate coding problem to practice the learned concepts, with an invitation to engage and a hint about the upcoming tutorial on the iter tools module.', 'duration': 107.587, 'highlights': ['The chapter covers the concept of iterators, including the functionality of iterable and iterator objects.', 'Announces the release of a separate coding problem to practice the learned concepts, with an invitation to engage.', 'Hints about the upcoming tutorial on the iter tools module.', 'Encourages engagement by inviting questions and offering support through various means such as liking, sharing, and contributing through Patreon.', 'Provides a clear understanding of what it means for something to be iterable and what it means for something to be an iterator.']}], 'duration': 320.22, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/jTYiNjvnHZY/pics/jTYiNjvnHZY1054856.jpg', 'highlights': ['Creating an iterator that runs indefinitely and counting up almost to a million values', 'Iterating through a generator function using a for loop prints values 1 through 10', 'Explaining the advantage of iterators in writing memory-efficient programs like password crackers', 'The chapter covers the concept of iterators, including the functionality of iterable and iterator objects', 'Provides a clear understanding of what it means for something to be iterable and what it means for something to be an iterator', 'Announces the release of a separate coding problem to practice the learned concepts, with an invitation to engage', 'Hints about the upcoming tutorial on the iter tools module', 'Encourages engagement by inviting questions and offering support through various means such as liking, sharing, and contributing through Patreon']}], 'highlights': ['Creating custom iterable classes and adding dunder iter and dunder next methods allows objects to be iterable and behave like built-in functions such as range', 'The class created is both iterable and an iterator, enabling iteration through values 1 to 10.', 'Creating an iterator that runs indefinitely and counting up almost to a million values', 'The video explains that an iterable is something that can be looped over, such as a list, helping viewers understand the concept and write better code.', 'The chapter covers the concept of iterators, including the functionality of iterable and iterator objects', 'Understanding these underlying concepts will help write more efficient code and see solutions to certain problems in a new way.', 'An iterator is an object with a state so that it remembers where it is during iteration, and it knows how to get its next value with a dunder next method', 'Emphasizing the significance of iterators having a state and proper iteration logic.', 'Explaining the advantage of iterators in writing memory-efficient programs like password crackers', 'Generators are introduced as useful for creating easy-to-read iterators.']}