Python Forum
Finding value in nested dictionaries with lists
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Finding value in nested dictionaries with lists
#6
(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)
Reply


Messages In This Thread
RE: Finding value in nested dictionaries with lists - by mart79 - Mar-08-2020, 12:24 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  List all possibilities of a nested-list by flattened lists sparkt 1 930 Feb-23-2023, 02:21 PM
Last Post: sparkt
  Split dict of lists into smaller dicts of lists. pcs3rd 3 2,387 Sep-19-2020, 09:12 AM
Last Post: ibreeden
  Comparing items from 2 lists of dictionaries illwill 7 2,799 Sep-14-2020, 10:46 PM
Last Post: bowlofred
  Searching through Nested Dictionaries and Lists Dave_London 1 7,833 Jul-09-2020, 03:36 PM
Last Post: mrdominikku
  Creating Nested Dictionaries Confusion gw1500se 2 2,158 May-18-2020, 11:16 PM
Last Post: gw1500se
  Unpacking nested lists yonatan776 1 2,219 Apr-14-2020, 08:50 PM
Last Post: buran
  Help: for loop with dictionary and nested lists mart79 1 1,875 Apr-12-2020, 02:52 PM
Last Post: TomToad
  nested dictionaries to CSV mart79 9 12,510 Jul-29-2019, 04:59 AM
Last Post: mart79
  Transform simplified dictionary to nested dictionaries bhojendra 1 2,383 Jul-02-2019, 02:05 PM
Last Post: ichabod801
  sort lists of lists with multiple criteria: similar values need to be treated equal stillsen 2 3,296 Mar-20-2019, 08:01 PM
Last Post: stillsen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020