# Convert iterator (of tuples) to vector

**URL:** <https://discourse.julialang.org/t/convert-iterator-of-tuples-to-vector/71088>\
**Category:** General Usage\
**Tags:** array, vector, iterators\
**Created:** [November 7, 2021, 11:41am UTC](https://discourse.julialang.org/t/convert-iterator-of-tuples-to-vector/71088 "2021-11-07T11:41:20Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Torkel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/torkel/32/5030_2.png) [@Torkel](https://discourse.julialang.org/u/Torkel)\
**Post date:** [November 7, 2021, 11:41am UTC](https://discourse.julialang.org/t/convert-iterator-of-tuples-to-vector/71088/1 "2021-11-07T11:41:20Z")

</div>

I have an iterator, and I wish to convert it into an array. Normally, I could just do:

```julia
vec = [i for i in iter]

```

or

```julia
vec = map(i -> i, iter]

```

but now my iterator happens to contain tuples (of numbers). It is generated as:

```julia
iter = Iterators.product(1:10,1:10,1:10)

```

and if I now do either one of

```julia
vec = [i for i in iter]
vec = map(i -> i, iter]

```

I get a 3d array, which is awkward, since I want a vector. The only way I have found is to do:

```julia
vec = [];
foreach(i -> push!(vec,i), iter)

```

but this feels a little bit more convoluted than what should be possible. It feels like I am missing something here, which I don’t understand, which is why the first two approaches doesn’t work.

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [November 7, 2021, 11:57am UTC](https://discourse.julialang.org/t/convert-iterator-of-tuples-to-vector/71088/2 "2021-11-07T11:57:58Z")

</div>

You can use [`Iterators.flatten`](https://docs.julialang.org/en/v1/base/iterators/#Base.Iterators.flatten) and then [`collect`](https://docs.julialang.org/en/v1/base/collections/#Base.collect-Tuple%7BAny%7D) the result:

```julia
julia> iter = Iterators.product(1:10,1:10,1:10)
Base.Iterators.ProductIterator{Tuple{UnitRange{Int64}, UnitRange{Int64}, UnitRange{Int64}}}((1:10, 1:10, 1:10))

julia> collect(Iterators.flatten(iter))
3000-element Vector{Int64}:
  1
  1
  ⋮
 10
 10

```

---

<div class="post-metadata">

**Author:** ![trahflow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/trahflow/32/30585_2.png) [@trahflow](https://discourse.julialang.org/u/trahflow)\
**Post date:** [November 7, 2021, 12:02pm UTC](https://discourse.julialang.org/t/convert-iterator-of-tuples-to-vector/71088/3 "2021-11-07T12:02:14Z")

</div>

Also note that the reason _why_ collecting that iterator returns an array of size (10, 10, 10) is, that the Iterator returned by `Iterators.product` defines `size()`:

```julia
julia> size(iter)
(10, 10, 10)

```

See [this part of the documentation](https://docs.julialang.org/en/v1/manual/interfaces/#man-interface-iteration) for how the iterator interface works.

In other words: If `size()` were undefined (or would return `1000`) for that particular iterator, the result would be what you had expected.

---

<div class="post-metadata">

**Author:** ![Torkel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/torkel/32/5030_2.png) [@Torkel](https://discourse.julialang.org/u/Torkel)\
**Post date:** [November 7, 2021, 12:34pm UTC](https://discourse.julialang.org/t/convert-iterator-of-tuples-to-vector/71088/4 "2021-11-07T12:34:53Z")

</div>

Thanks, that helps explain what is going on. Is there a way to still get it as tuples though? E.g.

```julia
Iterators.product(1:10,1:10,1:10)

```

returns

```julia
1
1
1

```

but I want still the tuple values:

```julia
(1,1,1)
(2,1,1)
(3,1,1)
...

```

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [November 7, 2021, 12:40pm UTC](https://discourse.julialang.org/t/convert-iterator-of-tuples-to-vector/71088/5 "2021-11-07T12:40:35Z")

</div>

Okay, misunderstood what you wanted. Then you can use [`vec`](https://docs.julialang.org/en/v1/base/arrays/#Base.vec):

```julia
julia> iter = Iterators.product(1:10,1:10,1:10);

julia> vec(collect(iter))
1000-element Vector{Tuple{Int64, Int64, Int64}}:
 (1, 1, 1)
 (2, 1, 1)
 ⋮
 (9, 10, 10)
 (10, 10, 10)

```

---

<div class="post-metadata">

**Author:** ![Torkel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/torkel/32/5030_2.png) [@Torkel](https://discourse.julialang.org/u/Torkel)\
**Post date:** [November 7, 2021, 12:44pm UTC](https://discourse.julialang.org/t/convert-iterator-of-tuples-to-vector/71088/6 "2021-11-07T12:44:33Z")

</div>

> [@fredrikekre](#):
>
> `vec(collect(iter))`

Thanks! 🙂
