# Nested dictionary usage

**URL:** https://discourse.julialang.org/t/nested-dictionary-usage/37589
**Category:** General Usage
**Tags:** question
**Created:** [April 14, 2020, 8:41pm UTC](https://discourse.julialang.org/t/nested-dictionary-usage/37589 "2020-04-14T20:41:52Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![CH-8](https://avatars.discourse-cdn.com/v4/letter/c/41988e/32.png) [@CH-8](https://discourse.julialang.org/u/CH-8)
#### Post date: [April 14, 2020, 8:41pm UTC](https://discourse.julialang.org/t/nested-dictionary-usage/37589/1 "2020-04-14T20:41:52Z")

</div>

Hi,

I am trying to create a nested dictionary where integer tuple keys map to another dictionary with an integer key and a float value. When I try to add values to this data structure, I get a key error

```julia
Test = Dict{Tuple{Int64,Int64},Dict{Int64,Float64}}()
Test[(1,1)][1] = 1.0

```

```julia
Stacktrace:
 [1] getindex(::Dict{Tuple{Int64,Int64},Dict{Int64,Float64}}, ::Tuple{Int64,Int64}) at .\dict.jl:477
 [2] top-level scope at In[89]:3

```

If I create the data structure manually like so

```julia
Test2 = Dict( (1,1) => Dict(1 => 1.0))

```

I get the data structure I tried to create above

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

```

and you can retrieve the value like I tried to add a value initially

```julia
Test2[(1,1)][1]

```

```julia
1.0

```

If I try to add another value though, I get an error.

```julia
Test2[(1,2)][2] = 2.0

```

```julia
KeyError: key (1, 2) not found

Stacktrace:
 [1] getindex(::Dict{Tuple{Int64,Int64},Dict{Int64,Float64}}, ::Tuple{Int64,Int64}) at .\dict.jl:477
 [2] top-level scope at In[93]:1

```

Any help or guidance would be appreciated.

---

<div class="post-metadata">

### Author: ![tomerarnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomerarnon/32/3170_2.png) [@tomerarnon](https://discourse.julialang.org/u/tomerarnon)
#### Post date: [April 14, 2020, 8:52pm UTC](https://discourse.julialang.org/t/nested-dictionary-usage/37589/2 "2020-04-14T20:52:55Z")

</div>

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-repl
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)

---

<div class="post-metadata">

### Author: ![CH-8](https://avatars.discourse-cdn.com/v4/letter/c/41988e/32.png) [@CH-8](https://discourse.julialang.org/u/CH-8)
#### Post date: [April 14, 2020, 9:52pm UTC](https://discourse.julialang.org/t/nested-dictionary-usage/37589/3 "2020-04-14T21:52:54Z")

</div>

Thank you. Your answer works.  
It juat takes an extra few lines to execute what I was trying to do.

```julia
using DataStructures
using DataFrames
df = DataFrame(Tkey1 = [1,1,2,2,3,4],Tkey2 = [1,1,1,2,1,1],
IntKey = [1,2,1,1,1,1],Value = [1.0,2.0,4.0,2.2,3.0,4.1])

D = Dict{Tuple{Int64,Int64},Dict{Int64,Float64}}()

```

```julia
for i in 1:size(df)[1]
    D[(df.Tkey1[i],df.Tkey2[i])] = Dict{Int64,Float64}()
end
D

```

```julia
Dict{Tuple{Int64,Int64},Dict{Int64,Float64}} with 5 entries:
  (3, 1) => Dict{Int64,Float64}()
  (2, 2) => Dict{Int64,Float64}()
  (1, 1) => Dict{Int64,Float64}()
  (4, 1) => Dict{Int64,Float64}()
  (2, 1) => Dict{Int64,Float64}()

```

```julia
for i in 1:size(df)[1]
     D[(df.Tkey1[i],df.Tkey2[i])][df.IntKey[i]] = df.Value[i]
end
D

```

```julia
Dict{Tuple{Int64,Int64},Dict{Int64,Float64}} with 5 entries:
  (3, 1) => Dict(1=>3.0)
  (2, 2) => Dict(1=>2.2)
  (1, 1) => Dict(2=>2.0,1=>1.0)
  (4, 1) => Dict(1=>4.1)
  (2, 1) => Dict(1=>4.0)

```

---

<div class="post-metadata">

### Author: ![yha](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yha/32/3502_2.png) [@yha](https://discourse.julialang.org/u/yha)
#### Post date: [April 14, 2020, 9:56pm UTC](https://discourse.julialang.org/t/nested-dictionary-usage/37589/4 "2020-04-14T21:56:39Z")

</div>

It may be more convenient to use a `DefaultDict`:

```julia
julia> using DataStructures
julia> d = DefaultDict{Tuple{Int64,Int64},Dict{Int64,Float64}}(()->Dict{Int64,Float64}())
DefaultDict{Tuple{Int64,Int64},Dict{Int64,Float64},var"#8#9"} with 0 entries

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

julia> d[(1,2)][3]=4
4

julia> d
DefaultDict{Tuple{Int64,Int64},Dict{Int64,Float64},var"#8#9"} with 2 entries:
  (1, 2) => Dict(3=>4.0)
  (1, 1) => Dict{Int64,Float64}()

```

---

<div class="post-metadata">

### Author: ![CH-8](https://avatars.discourse-cdn.com/v4/letter/c/41988e/32.png) [@CH-8](https://discourse.julialang.org/u/CH-8)
#### Post date: [April 17, 2020, 9:55pm UTC](https://discourse.julialang.org/t/nested-dictionary-usage/37589/5 "2020-04-17T21:55:11Z")

</div>

Sorry for the slow reply.  
Thank you very much. Your solution is very useful.
