Array of tuples to tuple of arrays

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> 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.

2 Likes