# Unpack vector of tuples from broadcasted function

**URL:** <https://discourse.julialang.org/t/unpack-vector-of-tuples-from-broadcasted-function/83350>\
**Category:** New to Julia\
**Tags:** question, syntax\
**Created:** [June 26, 2022, 9:18am UTC](https://discourse.julialang.org/t/unpack-vector-of-tuples-from-broadcasted-function/83350 "2022-06-26T09:18:20Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![drarnau](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/drarnau/32/4476_2.png) [@drarnau](https://discourse.julialang.org/u/drarnau)\
**Post date:** [June 26, 2022, 9:18am UTC](https://discourse.julialang.org/t/unpack-vector-of-tuples-from-broadcasted-function/83350/1 "2022-06-26T09:18:20Z")

</div>

Hello,

I think this is really simple but I can’t figure it out.

I have a function which I broadcast, something like:

```julia
function gives3(x)
    return x^2, x^3, x^4
end

inputs = [1, 2, 3, 4, 5]

output = gives3.(inputs)

```

`output` is a `Vector{Tuple{Int64, Int64, Int64}}`. I want to unpack these tuples and have them in separate vectors, so that when I write (or something similar):

```julia
v1, v2, v3 = gives3.(inputs)

```

I’d like:

```julia
julia> v2
5-element Vector{Int64}:
   1
   8
  27
  64
 125

```

Any idea on how to make this happen? I’m OK changing how `gives3()` returns stuff as long as I can return 3 elements.

Thanks!

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [June 26, 2022, 9:23am UTC](https://discourse.julialang.org/t/unpack-vector-of-tuples-from-broadcasted-function/83350/2 "2022-06-26T09:23:12Z")

</div>

One way:

```julia
julia> v1, v2, v3 = [getindex.(output, i) for i ∈ 1:3]
3-element Vector{Vector{Int64}}:
 [1, 4, 9, 16, 25]
 [1, 8, 27, 64, 125]
 [1, 16, 81, 256, 625]

```

---

<div class="post-metadata">

**Author:** ![drarnau](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/drarnau/32/4476_2.png) [@drarnau](https://discourse.julialang.org/u/drarnau)\
**Post date:** [June 26, 2022, 9:28am UTC](https://discourse.julialang.org/t/unpack-vector-of-tuples-from-broadcasted-function/83350/3 "2022-06-26T09:28:09Z")

</div>

Thanks! I’d like to simply change the syntax without calling another function or looping over `output`.

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [June 26, 2022, 9:52am UTC](https://discourse.julialang.org/t/unpack-vector-of-tuples-from-broadcasted-function/83350/4 "2022-06-26T09:52:04Z")

</div>

try defining new method for arrays, using broadcast internally not externally to the function:

```julia
julia> function gives3(x::Array)
           return x.^2, x.^3, x.^4
       end
gives3 (generic function with 2 methods)
julia> inputs = [1, 2, 3, 4, 5]
5-element Vector{Int64}:
 1
 2
 3
 4
 5

julia> v1,v2,v3 = gives3(inputs)
([1, 4, 9, 16, 25], [1, 8, 27, 64, 125], [1, 16, 81, 256, 625])

julia> v2
5-element Vector{Int64}:
   1
   8
  27
  64
 125

```

---

<div class="post-metadata">

**Author:** ![yha](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yha/32/3502_2.png) [@yha](https://discourse.julialang.org/u/yha)\
**Post date:** [June 26, 2022, 12:28pm UTC](https://discourse.julialang.org/t/unpack-vector-of-tuples-from-broadcasted-function/83350/5 "2022-06-26T12:28:03Z")

</div>

The `Unzip` package implements this:

```julia
using Unzip

v1, v2, v3 = unzip(gives3.(inputs))

```

---

<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:** [June 26, 2022, 1:32pm UTC](https://discourse.julialang.org/t/unpack-vector-of-tuples-from-broadcasted-function/83350/6 "2022-06-26T13:32:59Z")

</div>

Ditto for the SplitApplyCombine package via the `invert()` function
