# Is it possible to have the last iterator change the fastest in Iterator.product?

**URL:** <https://discourse.julialang.org/t/is-it-possible-to-have-the-last-iterator-change-the-fastest-in-iterator-product/63039>\
**Category:** General Usage\
**Tags:** question\
**Created:** [June 16, 2021, 5:37pm UTC](https://discourse.julialang.org/t/is-it-possible-to-have-the-last-iterator-change-the-fastest-in-iterator-product/63039 "2021-06-16T17:37:08Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![curious](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/curious/32/7629_2.png) [@curious](https://discourse.julialang.org/u/curious)\
**Post date:** [June 16, 2021, 5:37pm UTC](https://discourse.julialang.org/t/is-it-possible-to-have-the-last-iterator-change-the-fastest-in-iterator-product/63039/1 "2021-06-16T17:37:08Z")

</div>

In Julia’s Iterators.product, the first iterator changes the fastest. Is there an easy way to get elements in an order, so that the last iterator changes the fastest? For example with three sequences, I obtain:

```julia
res = Iterators.product([:A, :B, :C], [:X, :Y], [1,2]) |> collect
3×2×2 Array{Tuple{Symbol, Symbol, Int64}, 3}:
[:, :, 1] =
 (:A, :X, 1) (:A, :Y, 1)
 (:B, :X, 1) (:B, :Y, 1)
 (:C, :X, 1) (:C, :Y, 1)

[:, :, 2] =
 (:A, :X, 2) (:A, :Y, 2)
 (:B, :X, 2) (:B, :Y, 2)
 (:C, :X, 2) (:C, :Y, 2)

```

If I get elements using `res[:]` I obtain:

```julia
julia> res[:]
12-element Vector{Tuple{Symbol, Symbol, Int64}}:
 (:A, :X, 1)
 (:B, :X, 1)
 (:C, :X, 1)
 (:A, :Y, 1)
 (:B, :Y, 1)
 (:C, :Y, 1)
 (:A, :X, 2)
 (:B, :X, 2)
 (:C, :X, 2)
 (:A, :Y, 2)
 (:B, :Y, 2)
 (:C, :Y, 2)

```

Instead, I want:

```julia
(:A, :X, 1)
 (:A, :X, 2)
 (:A, :Y, 1)
 (:A, :Y, 2)
 (:B, :X, 1)
 (:B, :X, 2)
 (:B, :Y, 2)
 (:B, :Y, 2)
(:C, :X, 1)
 (:C, :X, 2)
 (:C, :Y, 2)
 (:C, :Y, 2)
 

```

What is the easiest way to get that from the `res` variable? Thank you!

---

<div class="post-metadata">

**Author:** ![lostella](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lostella/32/356_2.png) [@lostella](https://discourse.julialang.org/u/lostella)\
**Post date:** [June 16, 2021, 5:58pm UTC](https://discourse.julialang.org/t/is-it-possible-to-have-the-last-iterator-change-the-fastest-in-iterator-product/63039/2 "2021-06-16T17:58:58Z")

</div>

May I suggest you use different elements in the three iterators for your example? I think that would make it clearer

---

<div class="post-metadata">

**Author:** ![Elrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elrod/32/22461_2.png) [@Elrod](https://discourse.julialang.org/u/Elrod)\
**Post date:** [June 16, 2021, 6:06pm UTC](https://discourse.julialang.org/t/is-it-possible-to-have-the-last-iterator-change-the-fastest-in-iterator-product/63039/3 "2021-06-16T18:06:07Z")

</div>

```julia
julia> res = Iterators.map(reverse, Iterators.product([:C, :D], [:C, :D], [:C, :D])) |> collect |> vec
8-element Vector{Tuple{Symbol, Symbol, Symbol}}:
 (:C, :C, :C)
 (:C, :C, :D)
 (:C, :D, :C)
 (:C, :D, :D)
 (:D, :C, :C)
 (:D, :C, :D)
 (:D, :D, :C)
 (:D, :D, :D)

```

---

<div class="post-metadata">

**Author:** ![lostella](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lostella/32/356_2.png) [@lostella](https://discourse.julialang.org/u/lostella)\
**Post date:** [June 16, 2021, 6:06pm UTC](https://discourse.julialang.org/t/is-it-possible-to-have-the-last-iterator-change-the-fastest-in-iterator-product/63039/4 "2021-06-16T18:06:10Z")

</div>

One way to do that is to use `reverse`:

```julia
reverse.(res)

```

But you’ll probably want to first reverse the order of the arguments to `product`, too.

---

<div class="post-metadata">

**Author:** ![curious](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/curious/32/7629_2.png) [@curious](https://discourse.julialang.org/u/curious)\
**Post date:** [June 16, 2021, 6:11pm UTC](https://discourse.julialang.org/t/is-it-possible-to-have-the-last-iterator-change-the-fastest-in-iterator-product/63039/5 "2021-06-16T18:11:13Z")

</div>

Thank you @Elrod and @lostella. This works for the example I had given earlier. On lostella’s suggestion I changed the example to have different elements. This “reverse” solution does not work for the modified example. Is there a solution that works for this modified example?

Actually using reverse along with reversing the order of arguments to the Iterators.product (as lostella suggested) works well.

Thank you.
