Loop with for loops and counters
for count, value in enumerate(values):
print(count, value)
In Python, a for
loop is done over an iterable, wich means that no
counter is need. But if you do want to have a variable that changes on each
loop, you enumarate()
instead of creating and incrementing the variable
manually
Since this is an iterator, you can't acces it's itmes by indices
Since Python sequence types are 0
based indexes, the default starting value
is 0. But you can pass an argument to set the starting value of the count
values = [a, b, c]
for count, value in enumerate(values, start=1):
print(count, value)
# 1 a
# 2 b
# 3 c
You can use Python's built-in next()
to get the next value of an iterator
like the enumerate instance
values = ["a", "b"]
enum_instance = enumerate(values)
next(enum_instance) # (1, 'a')