-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path30.Iterators.py
More file actions
148 lines (125 loc) Β· 3.94 KB
/
30.Iterators.py
File metadata and controls
148 lines (125 loc) Β· 3.94 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
"""
===========================
π Iterators in Python β Complete Notes
===========================
β
What is an Iterator?
- An iterator is an object that allows us to traverse (iterate) through a collection (like a list, tuple, string).
- Uses two main methods:
__iter__() β returns the iterator object itself.
__next__() β returns the next item, raises StopIteration when no more items are left.
β
Syntax:
iterable = [1, 2, 3]
iterator = iter(iterable) # get iterator object
print(next(iterator)) # get next element
print(next(iterator))
β
Difference Between Iterable & Iterator:
- Iterable: any object that can return an iterator (list, tuple, string, etc.)
- Iterator: the object that performs iteration, keeps track of the current position.
"""
# ---------------------------
# BASIC ITERATOR EXAMPLE
# ---------------------------
my_list = [1, 2, 3, 4, 5, 6]
print("Original List:", my_list)
# Create an iterator
iterator = iter(my_list)
print("Iterator Object Type:", type(iterator))
# Access elements one by one
print("First Element:", next(iterator))
print("Second Element:", next(iterator))
# Iterate using a loop
print("Remaining Elements using Loop:")
for item in iterator:
print(item)
print("-" * 50)
# ---------------------------
# STRING ITERATOR EXAMPLE
# ---------------------------
my_string = "Hello"
string_iterator = iter(my_string)
print("String Iterator Example:")
try:
while True:
print(next(string_iterator))
except StopIteration:
print("Reached end of string.")
print("-" * 50)
# ---------------------------
# CUSTOM ITERATOR CLASS
# ---------------------------
class CountDown:
"""Custom iterator to count down from a given number."""
def __init__(self, start):
self.start = start
def __iter__(self):
return self
def __next__(self):
if self.start <= 0:
raise StopIteration
current = self.start
self.start -= 1
return current
print("Custom Iterator Example (Countdown):")
countdown = CountDown(5)
for num in countdown:
print(num)
print("-" * 50)
# ---------------------------
# ITERATOR OVER DICTIONARY
# ---------------------------
students = {"name": "Nived", "age": 18, "grade": "A"}
print("Iterating Over Dictionary Keys:")
for key in iter(students):
print(f"{key} -> {students[key]}")
print("-" * 50)
# ---------------------------
# ITERATOR WITH TUPLES
# ---------------------------
my_tuple = (10, 20, 30)
tuple_iterator = iter(my_tuple)
print("Tuple Iterator Example:")
print(next(tuple_iterator))
print(next(tuple_iterator))
print(next(tuple_iterator))
print("-" * 50)
# ---------------------------
# REAL-WORLD USE CASE: ITERATOR FOR PAGINATION
# ---------------------------
class Paginator:
"""Simulates pagination using an iterator."""
def __init__(self, items, page_size):
self.items = items
self.page_size = page_size
self.current_index = 0
def __iter__(self):
return self
def __next__(self):
if self.current_index >= len(self.items):
raise StopIteration
page = self.items[self.current_index:self.current_index + self.page_size]
self.current_index += self.page_size
return page
print("Pagination Example:")
data = list(range(1, 21)) # data from 1 to 20
paginator = Paginator(data, page_size=5)
for page in paginator:
print("Page:", page)
print("-" * 50)
# ---------------------------
# MANUAL ITERATION WITH try/except
# ---------------------------
iterator = iter([100, 200, 300])
print("Manual Iteration with try/except:")
while True:
try:
print(next(iterator))
except StopIteration:
print("End of iterator reached.")
break
print("-" * 50)
"""
β
When to Use Iterators:
- When you need memory-efficient access to data (one item at a time).
- When you are working with streams, large datasets, or infinite sequences.
- When you want to implement custom sequence logic (like pagination, countdown, etc.)
"""