Looking for a key in a nested dict

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
           end
           return output
       end
3 Likes