# Flatten nested tuples to a tuple

**URL:** <https://discourse.julialang.org/t/flatten-nested-tuples-to-a-tuple/96880>\
**Category:** New to Julia\
**Created:** [March 31, 2023, 7:36am UTC](https://discourse.julialang.org/t/flatten-nested-tuples-to-a-tuple/96880 "2023-03-31T07:36:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ryofurue](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ryofurue/32/24531_2.png) [@ryofurue](https://discourse.julialang.org/u/ryofurue)\
**Post date:** [March 31, 2023, 7:36am UTC](https://discourse.julialang.org/t/flatten-nested-tuples-to-a-tuple/96880/1 "2023-03-31T07:36:19Z")

</div>

I thought I would find a straightforward answer but haven’t been able to locate one. Sorry if this is a stupid question: What’s the simplest way to flatten a nested tuple?

The problems is, I call a function which returns a tuple of a different number of elements depending on the argument and I need to add one element in an intermediate function and return them together:

```julia
func(flag) = flag ? (2,3,4) : (2,3,4,5)

function myfunc()
  a = 1
  b, c, d = func(true) # When you know # of elems.
  return (a, b, c, d) # Works
end

function myfunc2(flag)
  a = 1
  t = func(flag)
  return vcat(a, collect(t))
end

function myfunc3(flag)
  a = 1
  t = func(flag)
  return myflatten(a, t) # What function?
end

a, b, c, d = myfunc()
a, b, c, d = myfunc2(true)
a, b, c, d, e = myfunc3(false)

```

In the above toy example, `myfunc3()` uses a fictitious function `myflatten()`, but is there a function like that?

`myfunc2()` works fine. It converts the tuple to a vector and returns the concatenated vector. So, this would be the simplest solution if there is no existing function to flatten tuples.

---

<div class="post-metadata">

**Author:** ![ffevotte](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ffevotte/32/6587_2.png) [@ffevotte](https://discourse.julialang.org/u/ffevotte)\
**Post date:** [March 31, 2023, 7:47am UTC](https://discourse.julialang.org/t/flatten-nested-tuples-to-a-tuple/96880/2 "2023-03-31T07:47:16Z")

</div>

Simply [splatting](https://docs.julialang.org/en/v1/manual/faq/#...-splits-one-argument-into-many-different-arguments-in-function-calls) the tuple should be enough in your case:

```julia
julia> func(flag) = flag ? (2,3,4) : (2,3,4,5)
func (generic function with 1 method)

julia> a = 1
1

julia> t = func(true)
(2, 3, 4)

julia> (a, t...)
(1, 2, 3, 4)

```

---

<div class="post-metadata">

**Author:** ![ryofurue](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ryofurue/32/24531_2.png) [@ryofurue](https://discourse.julialang.org/u/ryofurue)\
**Post date:** [March 31, 2023, 8:07am UTC](https://discourse.julialang.org/t/flatten-nested-tuples-to-a-tuple/96880/3 "2023-03-31T08:07:27Z")

</div>

> splatting

!!! That’s the word that I should have used to search the Internet. Thank you! After all, my question _was_ stupid.
