# Iterate over deepest values in a nested Association

Is it possible to iterate over the values of the deepest layer of keys in a nested association? For example:

assoc = <| "a"->0, "b"-> <| "c"->"one" ,"d"->2 , "e"->3|>, "f"->4|>


Would it be possible to iterate over just the values of at the deepest level in this example {0,"one",2,3,4}? Or, possible to iterate over the deepest level of keys such that I could test the type for the value of each key?

Level[assoc, {-1}]
(*    {0, "one", 2, 3, 4}    *)

• That is pretty nifty. Thanks! Jun 27, 2019 at 21:02
assoc /. Association -> (Values@*List) // Flatten


{0, "one", 2, 3, 4}

When some values are non-atomic this approach and the deepest Level approach give different results:

assoc2 = <| "a" -> foo,  "b" -> <| "c" -> "one" , "d" -> 2 , "e" -> 3|>,
"f" -> {bar, foo}|>;

assoc2 /. Association -> (Values@*List) // Flatten


{foo, "one", 2, 3, bar, foo}

Level[assoc2, {-1}]


{0, "one", 2, 3, 1, 3}

Map[
AssociationMap[
keyTest[#[]] -> valueTest[#[]] &
],
<|"a" -> 0, "b" -> <|"c" -> "one", "d" -> 2, "e" -> 3|>, "f" -> 4|>,
{-2}
]

<|"a" -> 0,
"b" -> <|keyTest["c"] -> valueTest["one"], keyTest["d"] -> valueTest,
keyTest["e"] -> valueTest|>, "f" -> 4|>