iterate python code example
Example 1: loop through python object
for attr, value in k.__dict__.items():
print(attr, value)
Example 2: python return iteration number of for loop
for iteration, item in enumerate(iteratable_object):
print(iteration)
[iteration for iteration, item in enumerate(iteratable_object)]
your_list = ["a", "b", "c", "d"]
for iteration,item in enumerate(your_list):
(iteration, item)
--> (0, 'a')
(1, 'b')
(2, 'c')
(3, 'd')
[iteration for iteration, item in enumerate(your_list)]
--> [0, 1, 2, 3]
Example 3: what is iteration in python
Example 4: how to use iteration in python
>>> a = ['foo', 'bar', 'baz']
>>> for i in a:
... print(i)
...
foo
bar
baz
Example 5: enumerate dictionary python
mydict = {
"one": 1,
"two": 2,
"three": 3
}
for index, item in enumerate(mydict):
print(f"{item}: {mydict.get(item)}")
Example 6: how to use iteration in python
for i = 1 to 10
<loop body>