Iterator and Generator
Iterators and generators are powerful features in Python that allow you to work with sequences of data efficiently. In this tutorial, we will explore iterators and generators, how they work, and when
class MyIterator:
def __init__(self):
self.values = [1, 2, 3, 4, 5]
self.index = 0
def __iter__(self):
return self
def __next__(self):
if self.index < len(self.values):
result = self.values[self.index]
self.index += 1
return result
else:
raise StopIterationLast updated