# Drop iterator shape

**URL:** <https://discourse.julialang.org/t/drop-iterator-shape/82140>\
**Category:** General Usage\
**Tags:** question, iterators\
**Created:** [June 2, 2022, 2:57pm UTC](https://discourse.julialang.org/t/drop-iterator-shape/82140 "2022-06-02T14:57:40Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [June 2, 2022, 2:57pm UTC](https://discourse.julialang.org/t/drop-iterator-shape/82140/1 "2022-06-02T14:57:40Z")

</div>

I am looking for the recommended way to “drop” the shape property of an iterator. Eg for

```julia
itr = Iterators.product(1:5, 1:2)

```

I want `itr_flat` such that

```julia
collect(itr_flat) == vec(collect(itr))

```

without, of course, collecting it first.

It is relatively straightforward to implement a wrapper that does this, just curious if it is defined anywhere, or if there is a clever way to do it.

---

<div class="post-metadata">

**Author:** ![skleinbo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skleinbo/32/36080_2.png) [@skleinbo](https://discourse.julialang.org/u/skleinbo)\
**Post date:** [June 2, 2022, 3:11pm UTC](https://discourse.julialang.org/t/drop-iterator-shape/82140/2 "2022-06-02T15:11:34Z")

</div>

```julia
julia> itr_flat = Iterators.take(itr, length(itr))
Base.Iterators.Take{Base.Iterators.ProductIterator{Tuple{UnitRange{Int64}, UnitRange{Int64}}}}(Base.Iterators.ProductIterator{Tuple{UnitRange{Int64}, UnitRange{Int64}}}((1:5, 1:2)), 10)

julia> collect(itr_flat)
10-element Vector{Tuple{Int64, Int64}}:
 (1, 1)
 (2, 1)
 (3, 1)
 (4, 1)
 (5, 1)
 (1, 2)
 (2, 2)
 (3, 2)
 (4, 2)
 (5, 2)

```

works on iterators of predetermined length, but feels a bit hackish.

On the other hand, the shape matters only when collected, right? But then you might as well do `reshape(x,:)` on the result.

---

<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 2, 2022, 3:40pm UTC](https://discourse.julialang.org/t/drop-iterator-shape/82140/3 "2022-06-02T15:40:41Z")

</div>

What about this:

```julia
itr_flat = Iterators.flatten((Iterators.product(1:5, 1:2),))
collect(itr_flat)

10-element Vector{Tuple{Int64, Int64}}:
 (1, 1)
 (2, 1)
 (3, 1)
 (4, 1)
 (5, 1)
 (1, 2)
 (2, 2)
 (3, 2)
 (4, 2)
 (5, 2)

```

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [June 3, 2022, 12:20pm UTC](https://discourse.julialang.org/t/drop-iterator-shape/82140/4 "2022-06-03T12:20:54Z")

</div>

Thanks for both answers. They are nice workarounds, but I think that something like an `Iterators.vec` would convey semantics better. I will make a PR.

**EDIT** Silly me, there is [`IterTools.ivec`](https://juliacollections.github.io/IterTools.jl/stable/#ivec(xs)).

---

<div class="post-metadata">

**Author:** ![skleinbo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skleinbo/32/36080_2.png) [@skleinbo](https://discourse.julialang.org/u/skleinbo)\
**Post date:** [June 3, 2022, 1:01pm UTC](https://discourse.julialang.org/t/drop-iterator-shape/82140/5 "2022-06-03T13:01:05Z")

</div>

`IterTools`… Of course 🤦‍♂️ Good to know!

---

<div class="post-metadata">

**Author:** ![artemsolod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/artemsolod/32/20704_2.png) [@artemsolod](https://discourse.julialang.org/u/artemsolod)\
**Post date:** [June 3, 2022, 2:17pm UTC](https://discourse.julialang.org/t/drop-iterator-shape/82140/6 "2022-06-03T14:17:58Z")

</div>

Another hack is to use comprehension with `if true` clause

```julia
itr = Iterators.product(1:5, 1:2)
itr_flat = (x for x in itr if true)
collect(itr_flat) == vec(collect(itr))

```
