# How to reshape() using an array instead of a tuple / Alt: How to convert an array into a tuple

**URL:** <https://discourse.julialang.org/t/how-to-reshape-using-an-array-instead-of-a-tuple-alt-how-to-convert-an-array-into-a-tuple/29101>\
**Category:** General Usage\
**Tags:** question\
**Created:** [September 24, 2019, 8:14am UTC](https://discourse.julialang.org/t/how-to-reshape-using-an-array-instead-of-a-tuple-alt-how-to-convert-an-array-into-a-tuple/29101 "2019-09-24T08:14:28Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![danielc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielc/32/220_2.png) [@danielc](https://discourse.julialang.org/u/danielc)\
**Post date:** [September 24, 2019, 8:14am UTC](https://discourse.julialang.org/t/how-to-reshape-using-an-array-instead-of-a-tuple-alt-how-to-convert-an-array-into-a-tuple/29101/1 "2019-09-24T08:14:28Z")

</div>

Hello,

I have something like this:

```julia
data = [... long array of numbers ...]
dims = [1024,256,128] # (for example)

```

I want to reshape the data and ideally I would like to do this:

```julia
reshape(data,dims)

```

The problem I have is that **reshape()** wants the dimensions as a tuple and **dims** is an array (note that **dims** is unknown at the start of the program). So I have to **reshape()** to allow me to use an array, or convert the array into a tuple. For example, I could do this:

```julia
reshape(data,ntuple(j -> dims[j],length(dims)))

```

Technically this works. But I imagine that there has to be a cleaner way to reshape an array. I cannot imagine that anyone who designed Julia intended for people to run this just to reshape an array. So I’m wondering if there is an alternate recommended way to do what I want to do.

Cheers,  
Daniel

---

<div class="post-metadata">

**Author:** ![improbable22](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/improbable22/32/5464_2.png) [@improbable22](https://discourse.julialang.org/u/improbable22)\
**Post date:** [September 24, 2019, 8:16am UTC](https://discourse.julialang.org/t/how-to-reshape-using-an-array-instead-of-a-tuple-alt-how-to-convert-an-array-into-a-tuple/29101/2 "2019-09-24T08:16:43Z")

</div>

You can just say `Tuple(dims)`.

BTW the reason `reshape` wants a tuple while e.g. `sum` accepts a vector is that the length of `dims` is then part of the type, so that the type of the output array (e.g. `Array{Int,3}` for your `dims`) can be determined from the type of the inputs.

---

<div class="post-metadata">

**Author:** ![danielc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielc/32/220_2.png) [@danielc](https://discourse.julialang.org/u/danielc)\
**Post date:** [September 24, 2019, 8:21am UTC](https://discourse.julialang.org/t/how-to-reshape-using-an-array-instead-of-a-tuple-alt-how-to-convert-an-array-into-a-tuple/29101/3 "2019-09-24T08:21:47Z")

</div>

Thanks! That’s exactly what I need. I’d swear I tried that. I wonder what I did to get it wrong… Oh. I see what I did:

```julia
Tuple(3,4) # wrong
tuple(dims) # wrong
Tuple(dims) # yay!

```

---

<div class="post-metadata">

**Author:** ![improbable22](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/improbable22/32/5464_2.png) [@improbable22](https://discourse.julialang.org/u/improbable22)\
**Post date:** [September 24, 2019, 8:27am UTC](https://discourse.julialang.org/t/how-to-reshape-using-an-array-instead-of-a-tuple-alt-how-to-convert-an-array-into-a-tuple/29101/4 "2019-09-24T08:27:54Z")

</div>

Yea it’s a bit confusing. `tuple([1,2,3]...) == Tuple([1,2,3]) == ([1,2,3]...,) == tuple(1,2,3) == (1,2,3)`, maybe that’s the complete list?

---

<div class="post-metadata">

**Author:** ![danielc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielc/32/220_2.png) [@danielc](https://discourse.julialang.org/u/danielc)\
**Post date:** [September 24, 2019, 8:42am UTC](https://discourse.julialang.org/t/how-to-reshape-using-an-array-instead-of-a-tuple-alt-how-to-convert-an-array-into-a-tuple/29101/5 "2019-09-24T08:42:24Z")

</div>

Wow. What does the “…” mean?

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

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

julia> [1,2,3]...
ERROR: syntax: "..." expression outside call

```

---

<div class="post-metadata">

**Author:** ![Karajan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/karajan/32/8545_2.png) [@Karajan](https://discourse.julialang.org/u/Karajan)\
**Post date:** [September 24, 2019, 9:20am UTC](https://discourse.julialang.org/t/how-to-reshape-using-an-array-instead-of-a-tuple-alt-how-to-convert-an-array-into-a-tuple/29101/6 "2019-09-24T09:20:32Z")

</div>

It’s called [splatting](https://docs.julialang.org/en/v1/base/base/#...). Here, it basically “unpacks” the array so that instead of `tuple([1, 2, 3])` you have `tuple(1, 2, 3)` which gives the desired result: `(1, 2, 3)`.  
You can do the same if you have a function that takes multiple arguments but you have an array/tuple/named tuple with them in it: `f(a, b, c) = a * b + c` and then `f((1.0, 4.0, 5.3)...)`
