julia equivalent of python [] (aka list)

784 views
Skip to first unread message

Andreas Lobinger

unread,
May 24, 2016, 1:06:32 PM5/24/16
to julia-users
Hello colleagues,

it really feels strange to ask this, but what is the julia equivalent of python's list?

So.

  1. can by initialised empty
  2. subject of append and extend
  3. accepting Any entry
  4. foolproof usage in type definition... (my real problem seems to be here)


Wishing a happy day,

        Andreas

Stefan Karpinski

unread,
May 24, 2016, 1:11:50 PM5/24/16
to Julia Users
Since Julia 0.4 [] is what you're looking for.

Andreas Lobinger

unread,
May 24, 2016, 1:16:22 PM5/24/16
to julia-users
I tend to agree with you, however...

julia
> d = Any[]
0-element Array{Any,1}

julia
> type d1
       name
::AbstractString
       content
::Any[]
       
end
ERROR
: TypeError: d1: in type definition, expected Type{T}, got Array{Any,1}

Stefan Karpinski

unread,
May 24, 2016, 1:20:07 PM5/24/16
to Julia Users
That is an instance not the type itself. The type is Vector{Any}.

Isaiah Norton

unread,
May 24, 2016, 1:20:14 PM5/24/16
to julia...@googlegroups.com
`Any[]` constructs a list instance. The proper type signature is `::Array{Any,1}` (or if you don't absolutely need it, you can leave the member signature out of the type declaration)

Tamas Papp

unread,
May 24, 2016, 1:22:53 PM5/24/16
to julia...@googlegroups.com
You are mixing up the constructor and the type syntax. Just use
Vector{Any} in the type definition.

On Tue, May 24 2016, Andreas Lobinger wrote:

> I tend to agree with you, however...
>
> julia> d = Any[]
> 0-element Array{Any,1}
>
> julia> type d1
> name::AbstractString
> content::Any[]
> end
> ERROR: TypeError: d1: in type definition, expected Type{T}, got Array{Any,1}
>
> On Tuesday, May 24, 2016 at 7:11:50 PM UTC+2, Stefan Karpinski wrote:
>
> Since Julia 0.4 [] is what you're looking for.
>
> On Tue, May 24, 2016 at 1:06 PM, Andreas Lobinger <lobi...@gmail.com> wrote:
>
> Hello colleagues,
>
> it really feels strange to ask this, but what is the julia equivalent of python's list?
>
> So.
>
> 1 can by initialised empty
> 2 subject of append and extend
> 3 accepting Any entry
> 4 foolproof usage in type definition... (my real problem seems to be here)

Andreas Lobinger

unread,
May 25, 2016, 3:52:38 AM5/25/16
to julia-users
Hello colleagues,

thanks for all answers. And now the punchline: How do i find this in the documentation?

Kristoffer Carlsson

unread,
May 25, 2016, 5:10:37 AM5/25/16
to julia-users
Find what? That [] creates an Array{Any, 1}?

Int[] creates an Array{Int, 1}
Float64[] creates an Array{Float64, 1}
etc

Leaving out the type, the only thing possible is that it can hold anything which is Any.

DNF

unread,
May 25, 2016, 5:50:43 AM5/25/16
to julia-users
Is ::Array{Any, 1} the correct annotation?
>> hello(v::Vector{Any}) = println("Hello")
>> hello([2,'a'])
Hello
>> hello([2,2])
ERROR
: MethodError: no method matching hello(::Array{Int64,1})
 
in eval(::Module, ::Any) at /usr/local/Cellar/julia/HEAD/lib/julia/sys.dylib:-1


It only works for vectors that are specifically of type Vector{Any}. Vector{Int64} is not a subtype of Vector{Any}.


This works, however, even though Vector is not a subtype of Vector{Any}:

>> goodbye(v::Vector) = println("bye, bye")
goodbye
(generic function with 1 method)
>> goodbye([2,'a'])
bye
, bye
>>> goodbye([2,2])
bye
, bye

Milan Bouchet-Valat

unread,
May 25, 2016, 6:01:11 AM5/25/16
to julia...@googlegroups.com
Le mercredi 25 mai 2016 à 02:50 -0700, DNF a écrit :
> Is ::Array{Any, 1} the correct annotation?
> >> hello(v::Vector{Any}) = println("Hello")
> >> hello([2,'a']) 
> Hello
> >> hello([2,2]) 
> ERROR: MethodError: no method matching hello(::Array{Int64,1}) 
>  in eval(::Module, ::Any) at
> /usr/local/Cellar/julia/HEAD/lib/julia/sys.dylib:-1
>
> It only works for vectors that are specifically of type Vector{Any}.
> Vector{Int64} is not a subtype of Vector{Any}.
>
> This works, however, even though Vector is not a subtype of
> Vector{Any}:
> >> goodbye(v::Vector) = println("bye, bye")
> goodbye (generic function with 1 method) 
> >> goodbye([2,'a'])
> bye, bye 
> >>> goodbye([2,2]) 
> bye, bye
You're hitting type invariance. See
http://docs.julialang.org/en/latest/manual/types/#parametric-composite-types
and look for this term in the mailing list archives.


Regards

Mauro

unread,
May 25, 2016, 6:08:01 AM5/25/16
to julia...@googlegroups.com
On Wed, 2016-05-25 at 11:50, DNF <oyv...@gmail.com> wrote:
> Is ::Array{Any, 1} the correct annotation?
>>> hello(v::Vector{Any}) = println("Hello")
>>> hello([2,'a'])
> Hello
>>> hello([2,2])
> ERROR: MethodError: no method matching hello(::Array{Int64,1})
> in eval(::Module, ::Any) at /usr/local/Cellar/julia/HEAD/lib/julia/sys.dylib:-
> 1
>
>
> It only works for vectors that are specifically of type Vector{Any}. Vector
> {Int64} is not a subtype of Vector{Any}.

This is co-/contravariance: Vector{Int} (say) is not a subtype of
Vector{Any} even though Int<:Any.

Write
hello{T}(v::Vector{T}) = println("Hello")

but if T<:Any then your goodbye function is fine. If some tighter
constraints are needed then, e.g.

hello{T<:Integer}(v::Vector{T}) = println("Hello")

DNF

unread,
May 25, 2016, 6:33:40 AM5/25/16
to julia-users
I was pointing out that Vector{Any} is not a drop-in replacement for Python list, especially for type annotations.

DNF

unread,
May 25, 2016, 8:08:26 AM5/25/16
to julia-users


On Wednesday, May 25, 2016 at 11:50:43 AM UTC+2, DNF wrote:


This works, however, even though Vector is not a subtype of Vector{Any}:

>> goodbye(v::Vector) = println("bye, bye")
goodbye
(generic function with 1 method)
>> goodbye([2,'a'])
bye
, bye
>>> goodbye([2,2])
bye
, bye

This is a bit backwards. Vector{Any} is a subtype of Vector, of course!

Perhaps I was expecting too much. I was envisioning a list containing 'any' sort of elements, and that would include vectors with all types of elements. After checking I see that numpy arrays are not a subclass of list, so Vector{Any} is sort of analogous to python 'list'. It just requires some extra thinking to realize that Vector{'anything'} is not included in that group. 

Stefan Karpinski

unread,
May 25, 2016, 8:14:46 AM5/25/16
to Julia Users
Vector is the type you're looking for – the type which includes Vector{T} for any T. Vector{Any} is a vector whose element type is `Any` specifically, a much more specific type. We don't yet have a notation for Vector{<:T} but it will likely be something like that.

Tom Breloff

unread,
May 25, 2016, 8:44:58 AM5/25/16
to julia...@googlegroups.com
And to take that further, it's much better to use AbstractVector (with no parameters) in method defs as that can accept anything with the right shape. 
Reply all
Reply to author
Forward
0 new messages