# Package or Function to find and access keys in a nested dicts

**URL:** <https://discourse.julialang.org/t/package-or-function-to-find-and-access-keys-in-a-nested-dicts/91909>\
**Category:** New to Julia\
**Created:** [December 20, 2022, 5:55pm UTC](https://discourse.julialang.org/t/package-or-function-to-find-and-access-keys-in-a-nested-dicts/91909 "2022-12-20T17:55:22Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![ellocco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ellocco/32/31331_2.png) [@ellocco](https://discourse.julialang.org/u/ellocco)\
**Post date:** [December 20, 2022, 5:55pm UTC](https://discourse.julialang.org/t/package-or-function-to-find-and-access-keys-in-a-nested-dicts/91909/1 "2022-12-20T17:55:22Z")

</div>

Continuing the discussion from [Looking for a key in a nested dict](https://discourse.julialang.org/t/looking-for-a-key-in-a-nested-dict/17211/2):

> [@Looking for a key in a nested dict](https://discourse.julialang.org/t/looking-for-a-key-in-a-nested-dict/17211/2):
>
> ```julia
> function retrieve(dict, key_of_interest)
> for (key, value) in dict
> if key == key_of_interest
> return value
> end
> if value isa Dict
> return retrieve(value, key_of_interest)
> end
> end
> end
> 
> ```

This does not work for me, my Dict has three sub-layers of Dicts.  
In my case I can reach the value of interest by the following command:

```julia
molar_mass_ = hydrogen_dict["TRANSPORT"]["viscosity"]["dilute"]["molar_mass"]

```

If I call the function above as follows:

```julia
molar_mass_ = retrieve(hydrogen_dict, "molar_mass")

```

The result is:

```julia
 typeof(molar_mass_)
Nothing

```

---

<div class="post-metadata">

**Author:** ![stillyslalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stillyslalom/32/45687_2.png) [@stillyslalom](https://discourse.julialang.org/u/stillyslalom)\
**Post date:** [December 20, 2022, 6:03pm UTC](https://discourse.julialang.org/t/package-or-function-to-find-and-access-keys-in-a-nested-dicts/91909/2 "2022-12-20T18:03:35Z")

</div>

Are you sure your example is completely free of typos? It works fine for me:

```julia
julia> hydrogen_dict = Dict("TRANSPORT" => Dict("viscosity" => Dict("dilute" => Dict("molar_mass" => 2.016))))
Dict{String, Dict{String, Dict{String, Dict{String, Float64}}}} with 1 entry:
  "TRANSPORT" => Dict("viscosity"=>Dict("dilute"=>Dict("molar_mass"=>2.016)))

julia> retrieve(hydrogen_dict, "molar_mass")
2.016

```

---

<div class="post-metadata">

**Author:** ![fatteneder](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fatteneder/32/33991_2.png) [@fatteneder](https://discourse.julialang.org/u/fatteneder)\
**Post date:** [December 20, 2022, 6:11pm UTC](https://discourse.julialang.org/t/package-or-function-to-find-and-access-keys-in-a-nested-dicts/91909/3 "2022-12-20T18:11:10Z")

</div>

Note that

1. a `Dicts` elements are not sorted
2. and your function will only return the first key it encounters.

This could easily cause problems when you have duplicated keys on different levels of a nested dict.

---

<div class="post-metadata">

**Author:** ![ellocco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ellocco/32/31331_2.png) [@ellocco](https://discourse.julialang.org/u/ellocco)\
**Post date:** [December 20, 2022, 6:15pm UTC](https://discourse.julialang.org/t/package-or-function-to-find-and-access-keys-in-a-nested-dicts/91909/4 "2022-12-20T18:15:26Z")

</div>

Yes, probably no typos, as far as I can see ☹

```julia
using PyCall
teqp = PyCall.pyimport("teqp")
hydrogen_dict = teqp.collect_component_json(["Hydrogen"], string(teqp.get_datapath()))[1]

```

P.S.:  
Installation of “teqp”

```julia
ENV["CONDA_JL_USE_MINIFORGE"] = "1"
import Conda; Conda.pip("install", "teqp")

```

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [December 20, 2022, 6:17pm UTC](https://discourse.julialang.org/t/package-or-function-to-find-and-access-keys-in-a-nested-dicts/91909/5 "2022-12-20T18:17:52Z")

</div>

Maybe they aren’t `Dict` types? But rather something unique to this `PyCall` and python combination. What does your structure look like?

---

<div class="post-metadata">

**Author:** ![ellocco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ellocco/32/31331_2.png) [@ellocco](https://discourse.julialang.org/u/ellocco)\
**Post date:** [December 20, 2022, 6:19pm UTC](https://discourse.julialang.org/t/package-or-function-to-find-and-access-keys-in-a-nested-dicts/91909/6 "2022-12-20T18:19:49Z")

</div>

```julia
typeof(hydrogen_dict)
Dict{Any, Any}

```

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [December 20, 2022, 6:20pm UTC](https://discourse.julialang.org/t/package-or-function-to-find-and-access-keys-in-a-nested-dicts/91909/7 "2022-12-20T18:20:39Z")

</div>

And the nested ones?

---

<div class="post-metadata">

**Author:** ![ellocco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ellocco/32/31331_2.png) [@ellocco](https://discourse.julialang.org/u/ellocco)\
**Post date:** [December 20, 2022, 9:43pm UTC](https://discourse.julialang.org/t/package-or-function-to-find-and-access-keys-in-a-nested-dicts/91909/8 "2022-12-20T21:43:24Z")

</div>

```julia
transport_ = pureJSON["TRANSPORT"]
typeof(transport_)
Dict{Any, Any}
viscosity_ = transport_["viscosity"]
typeof(viscosity_)
Dict{Any, Any}
dilute_ = viscosity_["dilute"]
typeof(dilute_)
Dict{Any, Any}

```

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [December 20, 2022, 9:48pm UTC](https://discourse.julialang.org/t/package-or-function-to-find-and-access-keys-in-a-nested-dicts/91909/9 "2022-12-20T21:48:53Z")

</div>

Okay, then I think [Package or Function to find and access keys in a nested dicts - #2 by stillyslalom](https://discourse.julialang.org/t/package-or-function-to-find-and-access-keys-in-a-nested-dicts/91909/2) is probably the correct explanation.

---

<div class="post-metadata">

**Author:** ![ellocco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ellocco/32/31331_2.png) [@ellocco](https://discourse.julialang.org/u/ellocco)\
**Post date:** [December 20, 2022, 9:58pm UTC](https://discourse.julialang.org/t/package-or-function-to-find-and-access-keys-in-a-nested-dicts/91909/10 "2022-12-20T21:58:10Z")

</div>

Sorry, I saw that there are two versions of the function, [the second version](https://discourse.julialang.org/t/looking-for-a-key-in-a-nested-dict/17211/9)  
works fine.

I have added a check, if only one instance of the `key_of_interrest` is included in the Dict,  
if this is the case the output is a scalar:

```julia
function _retrieve(dict, key_of_interest, output = []) # default value is an empty array
    for (key, value) in dict
        println("key: ", key, ", typeof(key): ", typeof(key))
        if key == key_of_interest
            push!(output, value)
        end
        if value isa AbstractDict
            _retrieve(value, key_of_interest, output)
        end
    end
    if size(output)[1] == 1
        output = output[]
    end
    return output
end

```
