Iteration reverse in for loop

hi all
in python if i want to track reverse through the for loop i write this

for i in range(len(digits)-1,-1,-1):

there is some thing similar in julia ?

You can use

for i in 10:-1:1

Note that the range includes the bounds

6 Likes

reverse(1:10) is also sometimes nice!

5 Likes

True! It’s somewhat more idiomatic, too.

I would use

for i in reverse(eachindex(digits))
Iterators.reverse(itr)

is in general more memory-efficient because it is lazy and does not create a new array of the iterator.
However, for only 10 numbers this is not really significant :wink:

2 Likes

For ranges, such as 1:10 and eachindex there are no allocations with Base.reverse either.

5 Likes