# Array of tuples to tuple of arrays

**URL:** <https://discourse.julialang.org/t/array-of-tuples-to-tuple-of-arrays/16973>\
**Category:** New to Julia\
**Created:** [October 30, 2018, 3:01pm UTC](https://discourse.julialang.org/t/array-of-tuples-to-tuple-of-arrays/16973 "2018-10-30T15:01:32Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Paul\_Soderlind](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paul_soderlind/32/1753_2.png) [@Paul\_Soderlind](https://discourse.julialang.org/u/Paul_Soderlind)\
**Post date:** [October 30, 2018, 3:01pm UTC](https://discourse.julialang.org/t/array-of-tuples-to-tuple-of-arrays/16973/1 "2018-10-30T15:01:32Z")

</div>

Background: a function returns a tuple, and then I call on it using the dot syntax with an array as input. The output will be an array of tuples, but I want a tuple of arrays. My example and solution is below. The question: is this solution any good or are there obvious things to improve?

```julia
function fn1(a)
  x = a/2
  y = a*2
  return x, y
end

z = fn1.(1:3) #an array of tuples   
(x,y) = ntuple(i->getindex.(z,i),2) #a tuple of arrays 

```

---

<div class="post-metadata">

**Author:** ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)\
**Post date:** [October 30, 2018, 4:09pm UTC](https://discourse.julialang.org/t/array-of-tuples-to-tuple-of-arrays/16973/2 "2018-10-30T16:09:18Z")

</div>

I just always use StructArrays.jl for this type of stuff nowadays, so I’ll plug that here.

---

<div class="post-metadata">

**Author:** ![piever](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/piever/32/1815_2.png) [@piever](https://discourse.julialang.org/u/piever)\
**Post date:** [October 30, 2018, 11:02pm UTC](https://discourse.julialang.org/t/array-of-tuples-to-tuple-of-arrays/16973/3 "2018-10-30T23:02:58Z")

</div>

Yes, StructArrays can handle conversion from Array of structs (either struct, or `NamedTuple` or `Tuple`) to a `NamedTuple` of `Arrays` (in terms of storage, but it still allows accessing the rows). For example:

```julia
julia> using StructArrays

julia> v = [(1, "a"), (2, "b")];

julia> s = StructArray(v)
2-element StructArray{Tuple{Int64,String},1,NamedTuple{(:x1, :x2),Tuple{Array{Int64,1},Array{String,1}}}}:
 (1, "a")
 (2, "b")

julia> s[1]
(1, "a")

julia> values(StructArrays.columns(s))
([1, 2], ["a", "b"])

```

Keeping things stored as a a `StructArray` is a flexible solution as you get both the tuple of vectors if you need, but at the same time can access the rows as tuples.

---

<div class="post-metadata">

**Author:** ![y4lu](https://avatars.discourse-cdn.com/v4/letter/y/47e85d/32.png) [@y4lu](https://discourse.julialang.org/u/y4lu)\
**Post date:** [October 31, 2018, 1:01am UTC](https://discourse.julialang.org/t/array-of-tuples-to-tuple-of-arrays/16973/4 "2018-10-31T01:01:00Z")

</div>

There is a more direct route, if it just needs to apply to that function

```julia
function fn2(a)
  return a./2, a.*2
  end;

(x,y) = fn2(z)
```
