# Iterator flatten with nested tuples

**URL:** <https://discourse.julialang.org/t/iterator-flatten-with-nested-tuples/83339>\
**Category:** New to Julia\
**Tags:** iterators\
**Created:** [June 25, 2022, 7:37pm UTC](https://discourse.julialang.org/t/iterator-flatten-with-nested-tuples/83339 "2022-06-25T19:37:32Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jinml](https://avatars.discourse-cdn.com/v4/letter/j/91b2a8/32.png) [@jinml](https://discourse.julialang.org/u/jinml)\
**Post date:** [June 25, 2022, 7:37pm UTC](https://discourse.julialang.org/t/iterator-flatten-with-nested-tuples/83339/1 "2022-06-25T19:37:32Z")

</div>

I am trying to flatten the following Iterator:

```julia
julia> it=Iterators.product(1:3,1:4); collect(it)
3×4 Matrix{Tuple{Int64, Int64}}:
 (1, 1) (1, 2) (1, 3) (1, 4)
 (2, 1) (2, 2) (2, 3) (2, 4)
 (3, 1) (3, 2) (3, 3) (3, 4)

```

and hope to get an length 12 iterator that yields a pair of indices a time. However, `Iterators.flatten(it)` returns a length 24 iterator that yields one index a time:

```julia
julia> collect(Iterators.flatten(it))
24-element Vector{Int64}:
 1
 1
 2
...

```

So how to generate the effect of two indices at a time? I’m looking for an `iterator` that yields these pairs.

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [June 25, 2022, 7:54pm UTC](https://discourse.julialang.org/t/iterator-flatten-with-nested-tuples/83339/3 "2022-06-25T19:54:04Z")

</div>

If you don’t need the product, maybe this is what you can use:

```julia
julia> it = ( (i,j) for j in 1:4 for i in 1:3 )
Base.Iterators.Flatten{Base.Generator{UnitRange{Int64}, var"#22#23"}}(Base.Generator{UnitRange{Int64}, var"#22#23"}(var"#22#23"(), 1:4))

julia> collect(it)
12-element Vector{Tuple{Int64, Int64}}:
 (1, 1)
 (2, 1)
 (3, 1)
 (1, 2)
 (2, 2)
 (3, 2)
 (1, 3)
 (2, 3)
 (3, 3)
 (1, 4)
 (2, 4)
 (3, 4)

```

---

<div class="post-metadata">

**Author:** ![jinml](https://avatars.discourse-cdn.com/v4/letter/j/91b2a8/32.png) [@jinml](https://discourse.julialang.org/u/jinml)\
**Post date:** [June 25, 2022, 7:59pm UTC](https://discourse.julialang.org/t/iterator-flatten-with-nested-tuples/83339/4 "2022-06-25T19:59:31Z")

</div>

> [@oheil](#):
>
> `julia> it = ( (i,j) for j in 1:4 for i in 1:3 )`

Did not realize the extra `for` instead of a `,` to have a flattening effect. Thank you!

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [June 25, 2022, 8:17pm UTC](https://discourse.julialang.org/t/iterator-flatten-with-nested-tuples/83339/5 "2022-06-25T20:17:43Z")

</div>

> [@jinml](#):
>
> `collect(Iterators.flatten(it))`

Another way is to shield ` it` inside ` Ref()` or in a tuple:

```julia
collect(Iterators.flatten((it,)))

```
