python for loop set iterator back code example

Example 1: how to create a break in iterating a sequence with a specific set of values in python

>>> d = {'foo': 1, 'bar': 2, 'baz': 3}
>>> for k, v in d.items():
...     print('k =', k, ', v =', v)
...
k = foo , v = 1
k = bar , v = 2
k = baz , v = 3

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]