Constructing a Dict but not sure WHY it works

I am using example code to learn julia when I can. I have come across this line

my_dict = Dict( compt => Dict{ String, Any }() for compt in comp_list) 

I can see what it’s doing BUT the area I am uncertain about is

, Any}()

So my question boils down to

“why is the () for Any outside the } of the Dict?”

thank you

That’s not an () for Any, that’s just the () that instantiates an empty Dict:

julia> Dict{String, Any} # This is just a type, not an object of the type!
Dict{String, Any}

julia> Dict{String, Any}() # Adding the () actually creates an instance of this type
Dict{String, Any}()

julia> Dict(letter => Dict{String, Any}() for letter ∈ 'a':'e') # a dict comprehension creates a dict where the values are other dicts
Dict{Char, Dict{String, Any}} with 5 entries:
  'a' => Dict()
  'c' => Dict()
  'd' => Dict()
  'e' => Dict()
  'b' => Dict()

Hi @nilshg thank you so much AGAIN!! I am still plugging away here but can only do it spasmodically :slight_smile: Great explanation as always!

have an EXCELLENT weekend and stay out of storm Eunice :slight_smile:

1 Like