how to loop over iterator in python code example
Example 1: python how to use iter
s = 'hello'
s_iter = iter(s) #Changes the string to an iterator
print(next(s_iter)) #Will print h
print(next(s_iter)) #Will print e
print(next(s_iter)) #Will print l
print(next(s_iter)) #Will print l
print(next(s_iter)) #will print o
Example 2: python for loop iterator
import numpy as np
# With array cycling
arr = np.array([1,2,3,4,5,6,7,8,9])
for i in range(len(arr)):
# logic with iterator use (current logic replaces even numbers with zero)
if arr[i] % 2 == 0: arr[i] = 0
print(arr)
# Output: [1, 0, 3, 0, 5, 0, 7, 0 , 9]