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
#8
(Mar-08-2020, 01:31 PM)ibreeden Wrote: Hello Mart79,
No, that is not what I mean. I meant this:
from collections import OrderedDict # the data appears to contain an OrderedDict.

def recurse_object(obj_to_inspect, val_to_find, indexpath=""):
    if isinstance(obj_to_inspect, dict):
        for key, value in obj_to_inspect.items():
            recurse_object(value, val_to_find, indexpath + f"['{key}']")
    if isinstance(obj_to_inspect, list):
        for key, value in enumerate(obj_to_inspect):
            recurse_object(value, val_to_find, indexpath + f"[{key}]")
    if isinstance(obj_to_inspect, str):
        if obj_to_inspect == val_to_find:
            print(f"Value {val_to_find} found at {indexpath}")

dictionary = {'nestedA': OrderedDict([('Name', 'TestCase'), ('VarA', 'Local'), ('VarB', None), ('VarC', None), ('VarD', [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]), ('VarE', 'ABCD'), ('VarF', 10.0), ('VarG', False)]), 'nestedB': OrderedDict([('LocA', {'v1': 'ABCD', 'v2': None, 'v3': None, 'v4': [[[0, 1], [1, 1], [2, 2], [0, 4]], [[0, 2], [1, 2], [2, 3], [4, 4]], ['name', 'ABCD']]}), ('LocB', None), ('LocC', None), ('LocD', True), ('LocE', True), ('LocF', False), ('LocG', False)])}
recurse_object(dictionary, 'ABCD') 
Output:
Value ABCD found at ['nestedA']['VarE'] Value ABCD found at ['nestedB']['LocA']['v1'] Value ABCD found at ['nestedB']['LocA']['v4'][2][1]
By the way: You used dict = {'nestedA': ... etc. Never use reserved words (like dict) as a name of a variable.

@ ibreeden, thank you so much! Yes, I did not realized it at first that I was using a reserved word.
Unfortunately, I have a second problem. The dictionary contains object information from other objects which do not get searched through.
I can't explain it other than an example:

dictionary = <class 'dict'>: {'case': 'Test',......,'checker': <checking.check.assessment object at 0x0003432C2D5646EB8>
the checking.check.assessment contains object information, dictionaries, lists etc. too.

How do I search through these objects for the same 'ABCD'?
Reply


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

Possibly Related Threads…
Thread Author Replies Views Last Post
  List all possibilities of a nested-list by flattened lists sparkt 1 947 Feb-23-2023, 02:21 PM
Last Post: sparkt
  Split dict of lists into smaller dicts of lists. pcs3rd 3 2,439 Sep-19-2020, 09:12 AM
Last Post: ibreeden
  Comparing items from 2 lists of dictionaries illwill 7 2,831 Sep-14-2020, 10:46 PM
Last Post: bowlofred
  Searching through Nested Dictionaries and Lists Dave_London 1 8,293 Jul-09-2020, 03:36 PM
Last Post: mrdominikku
  Creating Nested Dictionaries Confusion gw1500se 2 2,172 May-18-2020, 11:16 PM
Last Post: gw1500se
  Unpacking nested lists yonatan776 1 2,228 Apr-14-2020, 08:50 PM
Last Post: buran
  Help: for loop with dictionary and nested lists mart79 1 1,890 Apr-12-2020, 02:52 PM
Last Post: TomToad
  nested dictionaries to CSV mart79 9 12,554 Jul-29-2019, 04:59 AM
Last Post: mart79
  Transform simplified dictionary to nested dictionaries bhojendra 1 2,395 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,318 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