Can I convert tuple to array?

6,089 views
Skip to first unread message

Martin Isaksson

unread,
Feb 24, 2012, 3:16:20 PM2/24/12
to juli...@googlegroups.com
Hi,

is there an easy way to convert a tuple to an array?

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

julia> convert(Array, a)
no method convert(Type{Array{T,N}},(Int64,Int64,Int64))
in method_missing at /Users/martisak/devel/julia/j/base.j:58
in convert at /Users/martisak/devel/julia/j/base.j:3

Thank you!

Kind regards,
Martin

Jeff Bezanson

unread,
Feb 24, 2012, 4:15:41 PM2/24/12
to juli...@googlegroups.com
Simplest is to use an argument splice: [a...]
Also a comprehension [ a[i] | i=1:length(a) ]

Scott Noel-Hemming

unread,
Jul 12, 2015, 9:54:46 AM7/12/15
to juli...@googlegroups.com, martin....@gmail.com
You can also do so using collect

julia> t=(1,2,3)
(1,2,3)
julia> a=collect(t)
...
julia> typeof(a)
3-element Array{Int64,1}

Scott Jones

unread,
Jul 12, 2015, 10:10:52 AM7/12/15
to juli...@googlegroups.com
Which way is the most performant, or considered "best julian practice"?
splice, comprehension, or collect?

Milktrader

unread,
Jul 13, 2015, 1:04:59 PM7/13/15
to juli...@googlegroups.com
I like collect() best since it will preserve the type information, provided the tuple elements are of the same type, while comprehensions don't.

Milktrader

unread,
Jul 13, 2015, 1:11:11 PM7/13/15
to juli...@googlegroups.com
[a...] also preserves type information and will promote to achieve a single type, while collect(a) drops down to Any, if all the types don't match. Another thing to consider is that the splice syntax is more fragile than the collect(a) method.

Pontus Stenetorp

unread,
Jul 14, 2015, 4:29:55 AM7/14/15
to juli...@googlegroups.com
On 13 July 2015 at 18:11, Milktrader <milkt...@gmail.com> wrote:
>
> [a...] also preserves type information and will promote to achieve a single
> type, while collect(a) drops down to Any, if all the types don't match.
> Another thing to consider is that the splice syntax is more fragile than the
> collect(a) method.

Confirmed on both a reasonably new master and 0.3.10. The reason is
that `collect` currently depends on `eltype` to get this right and
`eltype` returns `Any` for say `(17, 4711.0)`. This seems odd to me
given that doing type promotion on a tuple should be trivial, it makes
me suspect that there may be a design decision somewhere that I am
missing.

Pontus

Stefan Karpinski

unread,
Jul 14, 2015, 9:50:13 AM7/14/15
to juli...@googlegroups.com
What do you think the eltype of that tuple should be?

Pontus Stenetorp

unread,
Jul 14, 2015, 9:59:58 AM7/14/15
to juli...@googlegroups.com
On 14 July 2015 at 14:49, Stefan Karpinski <ste...@karpinski.org> wrote:
>
> What do you think the eltype of that tuple should be?

To me, tuples are essentially fixed size immutable vectors, it could
be my old Python bias shining through though. Thus, shouldn't
`eltype([17, 4711.0]) == eltype((17, 4711.0))` hold? Am I missing
something undesirable that would follow from this?

Pontus

Stefan Karpinski

unread,
Jul 14, 2015, 11:25:01 AM7/14/15
to juli...@googlegroups.com
When you write [17, 4711.0] the 17 gets converted to Float64 upon construction of the array. That's not the case for tuples. One wants all([isa(x,eltype(c)) for x in c]) to hold for all collections, which would fail if the eltype of a tuple was what you're proposing. It might make sense to have eltype((17, 4711.0)) == Union{Int,Float64}, however, instead of Any.

Pontus Stenetorp

unread,
Jul 14, 2015, 12:05:53 PM7/14/15
to juli...@googlegroups.com
On 14 July 2015 at 16:24, Stefan Karpinski <ste...@karpinski.org> wrote:
>
> When you write [17, 4711.0] the 17 gets converted to Float64 upon
> construction of the array. That's not the case for tuples. One wants
> all([isa(x,eltype(c)) for x in c]) to hold for all collections, which would
> fail if the eltype of a tuple was what you're proposing. It might make sense
> to have eltype((17, 4711.0)) == Union{Int,Float64}, however, instead of Any.

Thank you for correcting and clarifying. I agree that
`Union{Int,Float64}` is probably a better choice.

Pontus

John Myles White

unread,
Jul 14, 2015, 12:49:04 PM7/14/15
to juli...@googlegroups.com
I'd argue that tuples shouldn't even have an eltype defined.

-- John

Scott Jones

unread,
Jul 14, 2015, 1:30:53 PM7/14/15
to juli...@googlegroups.com


On Tuesday, July 14, 2015 at 11:25:01 AM UTC-4, Stefan Karpinski wrote:
When you write [17, 4711.0] the 17 gets converted to Float64 upon construction of the array. That's not the case for tuples. One wants all([isa(x,eltype(c)) for x in c]) to hold for all collections, which would fail if the eltype of a tuple was what you're proposing. It might make sense to have eltype((17, 4711.0)) == Union{Int,Float64}, however, instead of Any.

Yes, and that conversion for arrays is potentially lossy, a point I raised in #12128.
It doesn't even give a InexactError, which seems like a bug when you have
[1234567890123456789, 2.0]

Steven G. Johnson

unread,
Jul 24, 2015, 10:58:29 AM7/24/15
to julia-dev, johnmyl...@gmail.com


On Tuesday, July 14, 2015 at 12:49:04 PM UTC-4, John Myles White wrote:
I'd argue that tuples shouldn't even have an eltype defined.

Why not?  They are an iterable container with finite length, and the eltype will typically be computable at compile-time. 
Reply all
Reply to author
Forward
0 new messages