# Array to Tuple

**URL:** <https://discourse.julialang.org/t/array-to-tuple/9024>\
**Category:** New to Julia\
**Tags:** array, tuple, ntuple\
**Created:** [February 12, 2018, 11:19pm UTC](https://discourse.julialang.org/t/array-to-tuple/9024 "2018-02-12T23:19:43Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ivo\_welch](https://avatars.discourse-cdn.com/v4/letter/i/d2c977/32.png) [@ivo\_welch](https://discourse.julialang.org/u/ivo_welch)\
**Post date:** [February 12, 2018, 11:19pm UTC](https://discourse.julialang.org/t/array-to-tuple/9024/1 "2018-02-12T23:19:43Z")

</div>

dear julia experts. conceptual question. a 2013 stefan k message indicated `tuple( [1:5;] )`, but this now creates a tuple _holding_ an array, rather than a tuple _from_ the array. so what is the opposite of `[i for i in mytuple]` ?

I am also wondering about the display of

```julia
julia> tuple( [1:5;] )
([1, 2, 3, 4, 5],)

```

there is no second element in the tuple, so why the trailing comma?

---

<div class="post-metadata">

**Author:** ![traktofon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/traktofon/32/591_2.png) [@traktofon](https://discourse.julialang.org/u/traktofon)\
**Post date:** [February 12, 2018, 11:33pm UTC](https://discourse.julialang.org/t/array-to-tuple/9024/2 "2018-02-12T23:33:21Z")

</div>

One way to do this is splatting:

```julia
julia> tuple([1:5;]...)
(1, 2, 3, 4, 5)

```

---

<div class="post-metadata">

**Author:** ![Liso](https://avatars.discourse-cdn.com/v4/letter/l/898d66/32.png) [@Liso](https://discourse.julialang.org/u/Liso)\
**Post date:** [February 12, 2018, 11:47pm UTC](https://discourse.julialang.org/t/array-to-tuple/9024/3 "2018-02-12T23:47:18Z")

</div>

```julia
julia> ([1,2,3]...,)
(1, 2, 3)

# or

julia> Tuple([1,2,3])
(1, 2, 3)

```

Trailing comma is for distinguish things like:

```julia
julia> typeof( (1+2,) )
Tuple{Int64}

julia> typeof( (1+2) )
Int64

```

---

<div class="post-metadata">

**Author:** ![Elrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elrod/32/22461_2.png) [@Elrod](https://discourse.julialang.org/u/Elrod)\
**Post date:** [February 12, 2018, 11:47pm UTC](https://discourse.julialang.org/t/array-to-tuple/9024/4 "2018-02-12T23:47:39Z")

</div>

`ntuple(i -> i, Val{5}())` is type stable.  
Edit: you probably need Compat. Without it, `Val{5}` probably works. With, `Val(5)` does.

---

<div class="post-metadata">

**Author:** ![greg\_plowman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/greg_plowman/32/8100_2.png) [@greg\_plowman](https://discourse.julialang.org/u/greg_plowman)\
**Post date:** [February 12, 2018, 11:48pm UTC](https://discourse.julialang.org/t/array-to-tuple/9024/5 "2018-02-12T23:48:25Z")

</div>

> [@ivo\_welch](#):
>
> there is no second element in the tuple, so why the trailing comma?

Tuples with one element are displayed with a trailing comma to distinguish them from a single element “grouped” with parentheses.

Compare `([1, 2, 3, 4, 5],)` and `([1, 2, 3, 4, 5])` when entered at the REPL.
