# Anybody can show me the grammer for dict's dict in julia

**URL:** <https://discourse.julialang.org/t/anybody-can-show-me-the-grammer-for-dicts-dict-in-julia/21616>\
**Category:** General Usage\
**Tags:** question\
**Created:** [March 8, 2019, 2:41am UTC](https://discourse.julialang.org/t/anybody-can-show-me-the-grammer-for-dicts-dict-in-julia/21616 "2019-03-08T02:41:04Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![biofuture](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/biofuture/32/22124_2.png) [@biofuture](https://discourse.julialang.org/u/biofuture)\
**Post date:** [March 8, 2019, 2:41am UTC](https://discourse.julialang.org/t/anybody-can-show-me-the-grammer-for-dicts-dict-in-julia/21616/1 "2019-03-08T02:41:04Z")

</div>

Hi, as I would like to use dict’s dict to storage and visiting data, but I can not find the correct grammar to stat the dict’s dict, initiate it and visiting it.

Can anybody give some examples about how to do it in julia? In perl, we use a lot similar features like hash’hash; hash’s array.

Thank you very much.

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [March 8, 2019, 3:00am UTC](https://discourse.julialang.org/t/anybody-can-show-me-the-grammer-for-dicts-dict-in-julia/21616/2 "2019-03-08T03:00:47Z")

</div>

I’m not sure if this is what you are requesting, maybe it is.  
If you one entry and all keys share a type and all values share another type:

```julia
julia> dict = Dict([1 => "one", 2 => "two"])
Dict{Int64,String} with 2 entries:
  2 => "two"
  1 => "one"

julia> dict[2]
"two"

julia> collect(keys(dict))
2-element Array{Int64,1}:
 2
 1

julia> collect(values(dict))
2-element Array{String,1}:
 "two"
 "one"

```

To update the dict

```julia

julia> dict[3] = "three"
"three"

julia> dict[1] = "One"
"One"

julia> collect(values(dict))
3-element Array{String,1}:
 "two"  
 "three"
 "One"  

```

There is much more flexibility too.  
This is important reading:  
[Collections and Data Structures · The Julia Language](https://docs.julialang.org/en/v1/base/collections/#Dictionaries-1)

---

<div class="post-metadata">

**Author:** ![biofuture](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/biofuture/32/22124_2.png) [@biofuture](https://discourse.julialang.org/u/biofuture)\
**Post date:** [March 8, 2019, 8:46am UTC](https://discourse.julialang.org/t/anybody-can-show-me-the-grammer-for-dicts-dict-in-julia/21616/3 "2019-03-08T08:46:02Z")

</div>

This is just Dict, it is not dict’s dict

```julia
D1 = Dict(["a" => "b"])

D2 = Dict(["c" => "d"])

julia> dict = Dict([1 => D1, 2 => D2])
Dict{Int64,Dict{String,String}} with 2 entries:
  2 => Dict("c"=>"d")
  1 => Dict("a"=>"b")

julia> dict[1]["a"]
"b"

##init a dict's dict
julia> dic = Dict(String => Dict(String => String))
Dict{DataType,Dict{DataType,DataType}} with 1 entry:
  String => Dict(String=>String)

##assign value to it 
julia> dic["b"]["a"] = "This is a test of dict's dict"
ERROR: KeyError: key "b" not found
Stacktrace:
 [1] getindex(::Dict{DataType,Dict{DataType,DataType}}, ::String) at ./dict.jl:478
 [2] top-level scope at none:0

```

Errors here!!

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [March 8, 2019, 8:56am UTC](https://discourse.julialang.org/t/anybody-can-show-me-the-grammer-for-dicts-dict-in-julia/21616/4 "2019-03-08T08:56:59Z")

</div>

```julia
julia> dict = Dict()
Dict{Any,Any} with 0 entries

julia> dict["b"] = Dict("a" => "This is a test")
Dict{String,String} with 1 entry:
  "a" => "This is a test"

julia> dict["b"]["a"]
"This is a test"

```

---

<div class="post-metadata">

**Author:** ![Olivier\_Merchiers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/olivier_merchiers/32/4073_2.png) [@Olivier\_Merchiers](https://discourse.julialang.org/u/Olivier_Merchiers)\
**Post date:** [March 8, 2019, 9:00am UTC](https://discourse.julialang.org/t/anybody-can-show-me-the-grammer-for-dicts-dict-in-julia/21616/5 "2019-03-08T09:00:09Z")

</div>

Maybe not totally related but maybe this helps

> [@Looking for a key in a nested dict](https://discourse.julialang.org/t/looking-for-a-key-in-a-nested-dict/17211/9):
>
> If it ocurs multiple times, only the first appearance of the key is returned. Here is an implementation that takes into account multiple returns function retrieve(dict, key\_of\_interest, output = []) # default value is an empty array for (key, value) in dict if key == key\_of\_interest push!(output, value) end if value isa AbstractDict retrieve(value, key\_of\_interest, output) end …

---

<div class="post-metadata">

**Author:** ![biofuture](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/biofuture/32/22124_2.png) [@biofuture](https://discourse.julialang.org/u/biofuture)\
**Post date:** [March 8, 2019, 11:07pm UTC](https://discourse.julialang.org/t/anybody-can-show-me-the-grammer-for-dicts-dict-in-julia/21616/6 "2019-03-08T23:07:01Z")

</div>

Thanks

```julia
dicta = Dict()

dictb = Dict()

dicta["a"] = dictb

#init dictb with multi keys

dictb["c"] = "d"

dictb["e"] = "f"

dicta["a"]["c"] = "d"

dicta["a"]["e"] = "f"

```

It may work in this way. Not convenient at all! At perl, you can directly init dicta[“a”][“c”]

= “d”

and insert another key dicta[“a”][“e”] = “f”

Jeffrey Sarnoff via JuliaLang [julialang@discoursemail.com](mailto:julialang@discoursemail.com) 于2019年3月8日周五 下午8:02写道：

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [March 8, 2019, 11:37pm UTC](https://discourse.julialang.org/t/anybody-can-show-me-the-grammer-for-dicts-dict-in-julia/21616/7 "2019-03-08T23:37:50Z")

</div>

You can use the `DefaultDict` from DataStructures.jl to accomplish this:

```julia
julia> dicta = DefaultDict(Dict)
DefaultDict{Any,Any,UnionAll} with 0 entries

julia> dicta["a"]["c"] = "d"
"d"

julia> dicta["a"]["e"] = "f"
"f"

julia> dicta
DefaultDict{Any,Any,UnionAll} with 1 entry:
  "a" => Dict{Any,Any}("c"=>"d","e"=>"f")

```

Please quote your code with ````` triple backtick “fences” to format it in a way that’s easier for folks to read (and copy-paste).

---

<div class="post-metadata">

**Author:** ![thautwarm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thautwarm/32/37760_2.png) [@thautwarm](https://discourse.julialang.org/u/thautwarm)\
**Post date:** [March 9, 2019, 12:36am UTC](https://discourse.julialang.org/t/anybody-can-show-me-the-grammer-for-dicts-dict-in-julia/21616/8 "2019-03-09T00:36:05Z")

</div>

Why not simply use `Dict{String, Dict{String, String}}`?

```julia
dic = Dict(
  "b" => Dict("a", "This is a test of dict of dict")
)

default(::Type{Dict{K, V}}) where {K, V} = Dict{K, V}()
default(::Type{Int}) = 0
default(::Type{String}) = ""

setter(k) = v -> dic -> dic[k] = v

getter(k :: K) where K = function (dic :: Dict{K, V}) where V
   get!(dic, k) do
       default(V)
   end
end

set1(v) = setter("a")(v) \circ getter("b")
set1("dict.b.a")(dic)

set2(v) = setter("c")(v) \circ getter("b")
set2("dict.b.c")(dic)

```

---

<div class="post-metadata">

**Author:** ![biofuture](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/biofuture/32/22124_2.png) [@biofuture](https://discourse.julialang.org/u/biofuture)\
**Post date:** [March 10, 2019, 9:53pm UTC](https://discourse.julialang.org/t/anybody-can-show-me-the-grammer-for-dicts-dict-in-julia/21616/9 "2019-03-10T21:53:22Z")

</div>

Thanks, this solution is works well and can work on the fly! Also for the tip of quote code with ```
