# Convert tuple to array

**URL:** <https://discourse.julialang.org/t/convert-tuple-to-array/2147>\
**Category:** General Usage\
**Tags:** question\
**Created:** [February 17, 2017, 6:41am UTC](https://discourse.julialang.org/t/convert-tuple-to-array/2147 "2017-02-17T06:41:10Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![holocronweaver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/holocronweaver/32/621_2.png) [@holocronweaver](https://discourse.julialang.org/u/holocronweaver)\
**Post date:** [February 17, 2017, 6:41am UTC](https://discourse.julialang.org/t/convert-tuple-to-array/2147/1 "2017-02-17T06:41:10Z")

</div>

Is there a way to quickly convert a tuple to an array?

A common use case is when I want to perform calculations on `size(my_array)`, say dividing the size by half or other more complex array operations.

Currently I expand the tuple like `dx, dy, dz = size(my_array); dims = [dx, dy, dz]` but that gets tedious after doing it 100+ times. I would much rather have `size(my_array)` return an array, or at the very least have a lazy way to convert it to a array.

---

<div class="post-metadata">

**Author:** ![PabloZubieta](https://avatars.discourse-cdn.com/v4/letter/p/ee7513/32.png) [@PabloZubieta](https://discourse.julialang.org/u/PabloZubieta)\
**Post date:** [February 17, 2017, 6:49am UTC](https://discourse.julialang.org/t/convert-tuple-to-array/2147/2 "2017-02-17T06:49:09Z")

</div>

I guess you could do `map(x -> x ÷ 2, size(my_array))`

---

<div class="post-metadata">

**Author:** ![holocronweaver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/holocronweaver/32/621_2.png) [@holocronweaver](https://discourse.julialang.org/u/holocronweaver)\
**Post date:** [February 17, 2017, 7:08am UTC](https://discourse.julialang.org/t/convert-tuple-to-array/2147/3 "2017-02-17T07:08:14Z")

</div>

Sorry, I should have made it more clear. I gave division by two as an example, but I have more complicated array operations in mind. Thus I need the dimensions in an array.

---

<div class="post-metadata">

**Author:** ![PabloZubieta](https://avatars.discourse-cdn.com/v4/letter/p/ee7513/32.png) [@PabloZubieta](https://discourse.julialang.org/u/PabloZubieta)\
**Post date:** [February 17, 2017, 7:13am UTC](https://discourse.julialang.org/t/convert-tuple-to-array/2147/4 "2017-02-17T07:13:14Z")

</div>

If it is the same operation on every element you still can easily use `map`. Just define a function that does the operation you want and map it over the tuple.

If you really need an array you can do `[i for i in tuple]` to create an array from the elements in the tuple.

---

<div class="post-metadata">

**Author:** ![holocronweaver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/holocronweaver/32/621_2.png) [@holocronweaver](https://discourse.julialang.org/u/holocronweaver)\
**Post date:** [February 17, 2017, 7:25am UTC](https://discourse.julialang.org/t/convert-tuple-to-array/2147/5 "2017-02-17T07:25:21Z")

</div>

The latter is what I was looking for. Completely overlooked comprehensions. Thank you!

---

<div class="post-metadata">

**Author:** ![lstagner](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lstagner/32/448_2.png) [@lstagner](https://discourse.julialang.org/u/lstagner)\
**Post date:** [February 17, 2017, 8:08am UTC](https://discourse.julialang.org/t/convert-tuple-to-array/2147/6 "2017-02-17T08:08:08Z")

</div>

You could also use `collect(tuple)`

---

<div class="post-metadata">

**Author:** ![holocronweaver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/holocronweaver/32/621_2.png) [@holocronweaver](https://discourse.julialang.org/u/holocronweaver)\
**Post date:** [February 17, 2017, 8:21am UTC](https://discourse.julialang.org/t/convert-tuple-to-array/2147/7 "2017-02-17T08:21:34Z")

</div>

Ah, this is prefect! I haven’t seen `collect` used outside of ranges before.

I timed it for small tuples and this is much faster than a list comprehension:

`julia> @time(collect((1,2,3))) 0.000006 seconds (7 allocations: 320 bytes) 3-element Array{Int64,1}: 1 2 3 julia> @time([i for i in (1,2,3)]) 0.018129 seconds (13.05 k allocations: 584.068 KB) 3-element Array{Int64,1}: 1 2 3`

---

<div class="post-metadata">

**Author:** ![fengyang.wang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fengyang.wang/32/104_2.png) [@fengyang.wang](https://discourse.julialang.org/u/fengyang.wang)\
**Post date:** [February 17, 2017, 8:26am UTC](https://discourse.julialang.org/t/convert-tuple-to-array/2147/8 "2017-02-17T08:26:18Z")

</div>

This timing is not accurate. Always time in a function and run more than once.

```julia
julia> f(tup) = @time collect(tup)
f (generic function with 1 method)

julia> f((1, 2, 3))
  0.000001 seconds (1 allocation: 112 bytes)
3-element Array{Int64,1}:
 1
 2
 3

julia> f((1, 2, 3))
  0.000000 seconds (1 allocation: 112 bytes)
3-element Array{Int64,1}:
 1
 2
 3

julia> g(tup) = @time [i for i in tup]
g (generic function with 1 method)

julia> g((1, 2, 3))
  0.000001 seconds (1 allocation: 112 bytes)
3-element Array{Int64,1}:
 1
 2
 3

julia> g((1, 2, 3))
  0.000000 seconds (1 allocation: 112 bytes)
3-element Array{Int64,1}:
 1
 2
 3

```

Hence they are both equally efficient.

---

<div class="post-metadata">

**Author:** ![PabloZubieta](https://avatars.discourse-cdn.com/v4/letter/p/ee7513/32.png) [@PabloZubieta](https://discourse.julialang.org/u/PabloZubieta)\
**Post date:** [February 17, 2017, 8:27am UTC](https://discourse.julialang.org/t/convert-tuple-to-array/2147/9 "2017-02-17T08:27:33Z")

</div>

Both should perform the same. Do not benchmark in global scope. If you put them in a function, e.g. `foo(t) = collect(t)` and `bar(t) = [i for i in t]`, you should see about the same performance.

---

<div class="post-metadata">

**Author:** ![holocronweaver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/holocronweaver/32/621_2.png) [@holocronweaver](https://discourse.julialang.org/u/holocronweaver)\
**Post date:** [February 17, 2017, 8:49am UTC](https://discourse.julialang.org/t/convert-tuple-to-array/2147/10 "2017-02-17T08:49:00Z")

</div>

My mistake! I tested it via functions and as you say they are about the same. I am coming back to Julia after ~1 year apart and am a bit rusty.

---

<div class="post-metadata">

**Author:** ![innerlee](https://avatars.discourse-cdn.com/v4/letter/i/b19c9b/32.png) [@innerlee](https://discourse.julialang.org/u/innerlee)\
**Post date:** [February 17, 2017, 8:54am UTC](https://discourse.julialang.org/t/convert-tuple-to-array/2147/11 "2017-02-17T08:54:51Z")

</div>

```julia
julia> a = (2,3,4)
(2,3,4)

julia> b = [2,3,4]
3-element Array{Int64,1}:
 2
 3
 4

julia> [a...]
3-element Array{Int64,1}:
 2
 3
 4

julia> (b...)
(2,3,4)

```

---

<div class="post-metadata">

**Author:** ![holocronweaver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/holocronweaver/32/621_2.png) [@holocronweaver](https://discourse.julialang.org/u/holocronweaver)\
**Post date:** [February 17, 2017, 9:02am UTC](https://discourse.julialang.org/t/convert-tuple-to-array/2147/12 "2017-02-17T09:02:02Z")

</div>

Ooh, is this the fathomed ‘splat’ I have been reading about? Didn’t think it could be used this way. Very concise!

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [February 17, 2017, 9:34am UTC](https://discourse.julialang.org/t/convert-tuple-to-array/2147/13 "2017-02-17T09:34:12Z")

</div>

```julia
julia> using BenchmarkTools

julia> a = (1, 2, 3)
(1,2,3)

julia> a_large = ([rand(1:5) for i in 1:50]...)
(2,1,2,1,2,1,3,5,1,1,5,4,1,5,3,4,2,2,3,3,2,3,5,1,4,4,5,2,2,3,1,2,5,2,1,2,3,2,2,4,1,3,1,4,2,3,5,2,4,1)

julia> f_splat(a) = [a...];

julia> f_collect(a) = collect(a);

julia> f_comp(a) = [i for i in a];

julia> @btime f_splat($a);
  35.741 ns (1 allocation: 112 bytes)

julia> @btime f_collect($a);
  31.897 ns (2 allocations: 128 bytes)

julia> @btime f_comp($a);
  26.184 ns (1 allocation: 112 bytes)

julia> @btime f_splat($a_large);
  774.571 ns (2 allocations: 992 bytes)

julia> @btime f_collect($a_large);
  82.332 ns (2 allocations: 560 bytes)

julia> @btime f_comp($a_large);
  88.702 ns (1 allocation: 544 bytes)

```

---

<div class="post-metadata">

**Author:** ![holocronweaver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/holocronweaver/32/621_2.png) [@holocronweaver](https://discourse.julialang.org/u/holocronweaver)\
**Post date:** [February 17, 2017, 9:57am UTC](https://discourse.julialang.org/t/convert-tuple-to-array/2147/14 "2017-02-17T09:57:18Z")

</div>

Nice! Seems best to avoid splats for large tuples, but otherwise any choice is roughly equally performant. As a bonus, BenchmarkTools will be very handy to me.

Maybe mention of these methods and their relative performance should go in the official docs?

---

<div class="post-metadata">

**Author:** ![assia855](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/assia855/32/22845_2.png) [@assia855](https://discourse.julialang.org/u/assia855)\
**Post date:** [May 19, 2021, 11:12am UTC](https://discourse.julialang.org/t/convert-tuple-to-array/2147/15 "2021-05-19T11:12:46Z")

</div>

I’m trying to implement this equation: P = A’\*D  
but the type of:

```julia
typeof(A)=LazyAlgebra.Composition{3,Tuple{LazyAlgebra.Inverse{LazyAlgebra.FFTs.FFTOperator{Float64,2,Complex{Float64},FFTW.rFFTWPlan{Float64,-1,false,2,UnitRange{Int64}},FFTW.rFFTWPlan{Complex{Float64},1,false,2,UnitRange{Int64}}}},LazyAlgebra.NonuniformScaling{Array{Complex{Float64},2}},LazyAlgebra.FFTs.FFTOperator{Float64,2,Complex{Float64},FFTW.rFFTWPlan{Float64,-1,false,2,UnitRange{Int64}},FFTW.rFFTWPlan{Complex{Float64},1,false,2,UnitRange{Int64}}}}}

typeof(D)=Array{Float64,3}

```

I tried to convert A like:

```julia
A = [A...]

```

to convert it and then transpose it, but I still have an error.

can you help me please?
