# Convert array of tuples to vector

**URL:** <https://discourse.julialang.org/t/convert-array-of-tuples-to-vector/31603>\
**Category:** General Usage\
**Tags:** question, tuple, data\_structures\
**Created:** [November 28, 2019, 12:02am UTC](https://discourse.julialang.org/t/convert-array-of-tuples-to-vector/31603 "2019-11-28T00:02:36Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![sboysel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sboysel/32/208407_2.png) [@sboysel](https://discourse.julialang.org/u/sboysel)\
**Post date:** [November 28, 2019, 12:02am UTC](https://discourse.julialang.org/t/convert-array-of-tuples-to-vector/31603/1 "2019-11-28T00:02:36Z")

</div>

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](https://discourse.julialang.org/t/array-of-tuples/11435) but I was wondering if there is a more general or preferred way to do this.

```julia
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

```

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [November 28, 2019, 12:08am UTC](https://discourse.julialang.org/t/convert-array-of-tuples-to-vector/31603/2 "2019-11-28T00:08:05Z")

</div>

This gives you a matrix

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

```

You can just vec that

---

<div class="post-metadata">

**Author:** ![sboysel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sboysel/32/208407_2.png) [@sboysel](https://discourse.julialang.org/u/sboysel)\
**Post date:** [November 28, 2019, 12:34am UTC](https://discourse.julialang.org/t/convert-array-of-tuples-to-vector/31603/3 "2019-11-28T00:34:03Z")

</div>

Thanks @baggepinnen!

```julia
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

```

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [November 28, 2019, 12:45am UTC](https://discourse.julialang.org/t/convert-array-of-tuples-to-vector/31603/4 "2019-11-28T00:45:37Z")

</div>

This might be a more straightforward solution

```julia
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

```

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [November 28, 2019, 2:22am UTC](https://discourse.julialang.org/t/convert-array-of-tuples-to-vector/31603/5 "2019-11-28T02:22:47Z")

</div>

> [@sboysel](#):
>
> I’d like collapse or unpack this array into a single vector containing all elements of the tuples.

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

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

```

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [November 28, 2019, 3:46am UTC](https://discourse.julialang.org/t/convert-array-of-tuples-to-vector/31603/6 "2019-11-28T03:46:05Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [November 28, 2019, 3:49am UTC](https://discourse.julialang.org/t/convert-array-of-tuples-to-vector/31603/7 "2019-11-28T03:49:28Z")

</div>

Yep, much more 😃

```julia
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

```
