I want to create a dict with as keys Integers in the range 1:lines and as value an empty Vector of Int.
Coming from Python I found this the shortest way to initialize this Dict:
d::Dict{Int, Vector{Int}} = Dict([(k, []) for k in 1:lines])
It works but I was wondering is there a better or more Julia way to do this?
Note, the type annotation seems to be ignored when I run your code. Not sure why that is, but the RHS of your code creates a Dict{Int, Vector{Any}}, and it doesn’t automatically convert when you add that type annotation.
This is expected because assignments in Julia always just return the right-hand side. That means the return value of the expression d::Dict{Int, Vector{Int}} = Dict([(k, []) for k in 1:lines]) is unaffected by the type annotation on the left-hand side.
julia> function foo()
lines = 5
d::Dict{Int, Vector{Int}} = Dict([(k, []) for k in 1:lines])
d
end;