# Convert array of tuples to vector

I have an array of homogenous tuples (all the same length with elements of the same type). I’d like collapse or unpack this array into a single vector containing all elements of the tuples. Order does not matter in my case. I’ve seen some workarounds here but I was wondering if there is a more general or preferred way to do this.

julia> x = [(0, 1), (2, 3), (4, 5), (5, 6)]
4-element Array{Tuple{Int64,Int64},1}:
(0, 1)
(2, 3)
(4, 5)
(5, 6)

julia> x = sort(append!(first.(x), last.(x)))
8-element Array{Int64,1}:
0
1
2
3
4
5
5
6

julia> y = [0, 1, 2, 3, 4, 5, 5, 6]
julia> isequal(x, y)
true

This gives you a matrix

Base.Matrix(x::Vector{<:Tuple}) = reduce(hcat, getindex.(x,i) for i in eachindex(x[1]))

You can just vec that

2 Likes

Thanks @baggepinnen!

julia> x = [(0, 1), (2, 3), (4, 5), (5, 6)]
4-element Array{Tuple{Int64,Int64},1}:
(0, 1)
(2, 3)
(4, 5)
(5, 6)

julia> vec(reduce(hcat, getindex.(x,i) for i in eachindex(x[1])))
8-element Array{Int64,1}:
0
2
4
5
1
3
5
6

This might be a more straightforward solution

julia> [x[j] for x in x for j in eachindex(x)]
8-element Array{Int64,1}:
0
1
2
3
4
5
5
6
1 Like

This is exactly what the Iterators.flatten iterator function is for:

julia> collect(Iterators.flatten(x))
8-element Array{Int64,1}:
0
1
2
3
4
5
5
6
5 Likes

collect(Iterations.flatten(x)) should also be more performant.

Yep, much more

julia> x = [(0, 1), (2, 3), (4, 5), (5, 6)]
4-element Array{Tuple{Int64,Int64},1}:
(0, 1)
(2, 3)
(4, 5)
(5, 6)

julia> f(x) = [x[j] for x in x for j in eachindex(x)]
f (generic function with 1 method)

julia> @btime f(\$x)
126.964 ns (6 allocations: 320 bytes)
8-element Array{Int64,1}:
0
1
2
3
4
5
5
6

julia> @btime collect(Iterators.flatten(\$x))
41.206 ns (2 allocations: 160 bytes)
8-element Array{Int64,1}:
0
1
2
3
4
5
5
6