Mar-08-2020, 12:24 PM
(Mar-08-2020, 10:57 AM)ibreeden Wrote:isinstance()
is the key to the solution of your problem.
You should make a function that starts with identifying the type.
If the passed parameter is an instance of "dict" then iterate over the values. (Indeed as Larz60+ wrote:for key, value in outerdict.items()
.) For each value call the same function recursively.
If the passed parameter is an instance of "list" then iterate over the values. For each value call the same function recursively.
If the passed parameter is an instance of "str" then compare the value with the value you are looking for. If it is the same: BINGO.
The first parameter of the function should be the object (dictionary, list, ...) to be inspected.
The second parameter of the function should be the value to look for.
The third parameter must default to a null string and should contain the indexpath. For each iteration the indexpath has to be extended. You use the indexpath as an answer to where the value was found.
In the simplest form the function may print the indexpath when a match is found.
But a well formed function usually returns the answer. This then would be a list of values because you state the value may occur more than once. This list should also be passed as a parameter and returned as an answer. For each match found, the indexpath must be added to this list.
I hope this helps you.
I understand it partially but get stuck at recursively...
for key, value in dictionary.items(): if isinstance(value, (dict)): for key1, value1 in value.items(): if isinstance(value1, list): for index in value1: print(key, key1, index)