# Initiating a Tuple of Arrays

**URL:** <https://discourse.julialang.org/t/initiating-a-tuple-of-arrays/70325>\
**Category:** General Usage\
**Tags:** tuple, vector\
**Created:** [October 25, 2021, 10:33am UTC](https://discourse.julialang.org/t/initiating-a-tuple-of-arrays/70325 "2021-10-25T10:33:43Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![mrVeng](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mrveng/32/8836_2.png) [@mrVeng](https://discourse.julialang.org/u/mrVeng)\
**Post date:** [October 25, 2021, 10:33am UTC](https://discourse.julialang.org/t/initiating-a-tuple-of-arrays/70325/1 "2021-10-25T10:33:43Z")

</div>

Dear all,

I would like to initiate a Tuple of Vectors. I usually use `map` in this case as the return type is often of type `Tuple`, but for some reason the following MWE outputs a Vector of Vector instead of a Tuple of Vectors:

```julia
function initvec(T, sz)
        return Vector{T}(undef, sz)
end

function tuplevecs(T, inner, outer)
        return map(iter -> initvec(T, inner), 1:outer)
end

initvec(Float64, 10) #Vector{Float}
tupvec = tuplevecs(Float64, 10, 100) #Vector{Vector{Float64}}
typeof(tupvec) #Vector{Vector{Float64}}

```

I can manually change the type via `Tuple(tupvec)`, but that seems inefficient.

**Question** : Is there a way to initialize a Tuple of Vectors via `map`? It does not really have to be map based either if there is another clever way to do what I intend to do.

---

<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:** [October 25, 2021, 10:49am UTC](https://discourse.julialang.org/t/initiating-a-tuple-of-arrays/70325/2 "2021-10-25T10:49:22Z")

</div>

Have you checked `ntuple`?

```julia
inner = 10; outer = 100
tupvec2 = ntuple( _ -> initvec(Float64,inner), outer)

julia> typeof(tupvec2)
NTuple{100, Vector{Float64}}

```

---

<div class="post-metadata">

**Author:** ![mrVeng](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mrveng/32/8836_2.png) [@mrVeng](https://discourse.julialang.org/u/mrVeng)\
**Post date:** [October 25, 2021, 11:04am UTC](https://discourse.julialang.org/t/initiating-a-tuple-of-arrays/70325/3 "2021-10-25T11:04:47Z")

</div>

> [@rafael.guerra](#):
>
> `ntuple( _ -> initvec(Float64,inner), outer)`

This works perfect, thank you!

---

<div class="post-metadata">

**Author:** ![StevenWhitaker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevenwhitaker/32/9749_2.png) [@StevenWhitaker](https://discourse.julialang.org/u/StevenWhitaker)\
**Post date:** [October 25, 2021, 1:55pm UTC](https://discourse.julialang.org/t/initiating-a-tuple-of-arrays/70325/4 "2021-10-25T13:55:07Z")

</div>

> [@mrVeng](#):
>
> for some reason the following MWE outputs a Vector of Vector instead of a Tuple of Vectors

From what I understand, to get a `Tuple` you need to call `map` with a `Tuple`. Example:

```julia
julia> map(i -> Vector{Bool}(undef, 2), 1:3) # map called with AbstractVector
3-element Vector{Vector{Bool}}:
 [0, 0]
 [0, 0]
 [0, 0]

julia> map(i -> Vector{Bool}(undef, 2), (1, 2, 3)) # map called with Tuple
(Bool[1, 0], Bool[0, 0], Bool[0, 0])

```

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [October 25, 2021, 4:07pm UTC](https://discourse.julialang.org/t/initiating-a-tuple-of-arrays/70325/5 "2021-10-25T16:07:21Z")

</div>

Yeah, I’m a bit curious why @mrVeng expected tuple output in the first place. Maybe there’s some misunderstanding?

---

<div class="post-metadata">

**Author:** ![mrVeng](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mrveng/32/8836_2.png) [@mrVeng](https://discourse.julialang.org/u/mrVeng)\
**Post date:** [October 25, 2021, 4:34pm UTC](https://discourse.julialang.org/t/initiating-a-tuple-of-arrays/70325/6 "2021-10-25T16:34:58Z")

</div>

It totally makes sense now, but I did not know that the return type is chosen based on the iterator.

---

<div class="post-metadata">

**Author:** ![mrVeng](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mrveng/32/8836_2.png) [@mrVeng](https://discourse.julialang.org/u/mrVeng)\
**Post date:** [October 25, 2021, 4:35pm UTC](https://discourse.julialang.org/t/initiating-a-tuple-of-arrays/70325/7 "2021-10-25T16:35:12Z")

</div>

Thank you, that explains everything really well!
