# Enumeration with shifted indices

``````enumerate(iter)
``````

is an iterator that yields (i, x) where i is a counter
starting at 1, and x is the i-th value from the given iterator.

How to define

``````enumerate(iter, start=0)
``````

an iterator that yields (i+start, x) where i is a counter
starting at 1, and x is the i-th value from the given iterator?

More generally, there is Base.Iterators.countfrom(start=1, step=1)
which is an iterator starting at start and incrementing by step.

How to define

``````enumerate(iter, start=0, step=1)
``````

which is an iterator that yields (i*step+start, x) where i
is a counter starting at 1, and x is the i-th value from the
given iterator?

Here is one way:

``````julia> iter = ("A", "B", "C")
("A", "B", "C")

julia> for (i, a) in zip(Iterators.countfrom(0), iter)
println("i = \$i, a = \$a")
end
i = 0, a = A
i = 1, a = B
i = 2, a = C

julia> for (i, a) in zip(Iterators.countfrom(0, 2), iter)
println("i = \$i, a = \$a")
end
i = 0, a = A
i = 2, a = B
i = 4, a = C
``````
5 Likes