# Reversing order of \`prod(itr)\`

**URL:** https://discourse.julialang.org/t/reversing-order-of-prod-itr/51613
**Category:** General Usage
**Created:** [December 10, 2020, 2:42pm UTC](https://discourse.julialang.org/t/reversing-order-of-prod-itr/51613 "2020-12-10T14:42:31Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![jlbosse](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlbosse/32/11274_2.png) [@jlbosse](https://discourse.julialang.org/u/jlbosse)
#### Post date: [December 10, 2020, 2:42pm UTC](https://discourse.julialang.org/t/reversing-order-of-prod-itr/51613/1 "2020-12-10T14:42:31Z")

</div>

Is there a way to make the product built by `prod(itr)` from right to left instead from left to right? So I want

```julia
julia> prod("$i" for i in [1,2,3]) == "123"

```

instead of

```julia
julia> prod("$i" for i in [1,2,3]) == "321"

```

?

Note that in my case [using `Iterators.reverse`](https://discourse.julialang.org/t/is-there-a-version-of-enumerate-in-the-reverse-order/38374) does not work, because I get `MethodError: no method matching iterate(::Base.Iterators.Reverse{Dict{Int64,String}}`

---

<div class="post-metadata">

### Author: ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)
#### Post date: [December 10, 2020, 2:52pm UTC](https://discourse.julialang.org/t/reversing-order-of-prod-itr/51613/2 "2020-12-10T14:52:49Z")

</div>

What collection are you actually trying to `prod` over? `reverse` on the input works fine for me:

```julia
julia> prod("$i" for i in Iterators.reverse([1,2,3]))
"321"

```

as does regular `reverse`:

```julia
julia> reverse(prod("$i" for i in [1,2,3]))
"321"

```

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [December 10, 2020, 2:53pm UTC](https://discourse.julialang.org/t/reversing-order-of-prod-itr/51613/3 "2020-12-10T14:53:27Z")

</div>

> [@jlbosse](#):
>
> Note that in my case [using `Iterators.reverse`](https://discourse.julialang.org/t/is-there-a-version-of-enumerate-in-the-reverse-order/38374) does not work, because I get `MethodError: no method matching iterate(::Base.Iterators.Reverse{Dict{Int64,String}}`

If you are using a dictionary, the iteration order is undefined and may change from release to release. So I think you need a different data structure if you want a specific order.

---

<div class="post-metadata">

### Author: ![jlbosse](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlbosse/32/11274_2.png) [@jlbosse](https://discourse.julialang.org/u/jlbosse)
#### Post date: [December 10, 2020, 3:38pm UTC](https://discourse.julialang.org/t/reversing-order-of-prod-itr/51613/4 "2020-12-10T15:38:19Z")

</div>

I guess I am out of luck then and need to do

```julia
str = ""
for i in [1,2,3]
    str = str * "$i"
end

```

because my iterator (a dict) happens to be ordered the way I want it. And since it is only for string representation this order is not a must, but more convenient to read.

---

<div class="post-metadata">

### Author: ![jlbosse](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlbosse/32/11274_2.png) [@jlbosse](https://discourse.julialang.org/u/jlbosse)
#### Post date: [December 10, 2020, 3:40pm UTC](https://discourse.julialang.org/t/reversing-order-of-prod-itr/51613/5 "2020-12-10T15:40:07Z")

</div>

Over `pair(Dict(Int => String))` and because of the way I create it, it usually happens to be iterated over in the “correct” order.

---

<div class="post-metadata">

### Author: ![FPGro](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fpgro/32/20822_2.png) [@FPGro](https://discourse.julialang.org/u/FPGro)
#### Post date: [December 10, 2020, 4:01pm UTC](https://discourse.julialang.org/t/reversing-order-of-prod-itr/51613/6 "2020-12-10T16:01:00Z")

</div>

Well you can `reverse(collect(mydict))` if efficiency is not a concern and if reverse unspecified order looks better than unspecified order

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [December 10, 2020, 7:59pm UTC](https://discourse.julialang.org/t/reversing-order-of-prod-itr/51613/7 "2020-12-10T19:59:42Z")

</div>

> [@jlbosse](#):
>
> because my iterator (a dict) happens to be ordered the way I want it.

You realize that you can’t rely on this? The ordering of a `Dict` may change unpredictably as you add elements, or if you update Julia to a new version. Don’t use `Dict` if you want a specific iteration order; use something else like the [`OrderedDict` type in the OrderedCollections.jl package](https://github.com/JuliaCollections/OrderedCollections.jl).

> [@jlbosse](#):
>
> ```julia
> str = ""
> for i in [1,2,3]
> str = str * "$i"
> end
> 
> ```

Don’t accumulate strings this way — every iteration of your loop allocates and copies into a new string. See this discussion: [Accumulating strings dynamically - #3 by stevengj](https://discourse.julialang.org/t/accumulating-strings-dynamically/51209/3)

---

<div class="post-metadata">

### Author: ![jlbosse](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlbosse/32/11274_2.png) [@jlbosse](https://discourse.julialang.org/u/jlbosse)
#### Post date: [December 11, 2020, 1:23pm UTC](https://discourse.julialang.org/t/reversing-order-of-prod-itr/51613/8 "2020-12-11T13:23:27Z")

</div>

Thanks for pointing out the thread about string accumulation! About the order: As @FPGro said the reverse, unspecified order looks better than the unspecified order, but the exact order is no hard constraint. Just a “nice to have”
