Nested dictionary usage

Your first dictionary has no keys or values in it (it’s blank). So you can’t index it at [(1,1)] at all in the first place.

julia> D = Dict{Tuple{Int64,Int64},Dict{Int64,Float64}}()
Dict{Tuple{Int64,Int64},Dict{Int64,Float64}} with 0 entries

julia> D[(1,1)]
ERROR: KeyError: key (1, 1) not found
Stacktrace:
 [1] getindex(::Dict{Tuple{Int64,Int64},Dict{Int64,Float64}}, ::Tuple{Int64,Int64}) at ./dict.jl:477
 [2] top-level scope at REPL[940]:1

julia> D[(1,1)] = Dict{Int, Float64}()
Dict{Int64,Float64} with 0 entries

julia> D[(1,1)][1] = 1.0
1.0

julia> D
Dict{Tuple{Int64,Int64},Dict{Int64,Float64}} with 1 entry:
  (1, 1) => Dict(1=>1.0)

To do something like D[key][1] = v, the value in D[key] must already be defined (since you’re indexing into it)

3 Likes