getting all values from a dict key

benniy's icon

hei folks,
i'm new to dictionaries and i've got one structured as following:

{
    "animals" :     {
        "monkey" : 21,
        "bear" : 22
    }
,
    "cars" :     {
        "fiat" : 23,
        "opel" : 24
    }

}

i'd like to return all values from the key "animals" (so basically "monkey 21" and "bear 22"). is it possible? banging and itering the dict only spits out its name – sending a "get animals" doesn't work, either.

thanks!

do.while's icon

Hi !
have u checked Dict help : hierarchy tab & array tab ?

benniy's icon

hei there,

yes i have. unfortunately it doesn't tell me how to get *all* keys from a certain directory. as for my example - "animals" being the top directory, i'd like to dump all it's keys and their values, but (get animals::) and the like does not work.

do.while's icon
Max Patch
Copy patch and select New From Clipboard in Max.

right !
your keys are dictionaries . so u have to refer to them . u should check dict.unpack and dict.iter it will help u to strip the keys !

do.while's icon
Max Patch
Copy patch and select New From Clipboard in Max.

or directly

benniy's icon

oh great! that was pretty easy! - thanks so much.

Arthur's icon

Very helpful, much appreciated!

Riccardo Santalucia's icon
Isaac Ray's icon

Is there no way to extract just the keys from sub-dictionaries? Having to iterate over everything in your sub-dictionaries and then get your keys seems fairly inefficient, especially as each item in my dictionary is a long list.

tyler mazaika's icon

“get” the sub-dictionary, put it into a [dict] object and call “getkeys”?

Isaac Ray's icon

No worries, just tried Do.While's example again and it's perfect.