Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filtering out Python Dictionary Values with Array of Nested Keys

I am trying to filter out a number of values from a python dictionary. Based on the answer seen here: Filter dict to contain only certain keys. I am doing something like:

new = {k:data[k] for k in FIELDS if k in data}

Basically create the new dictionary and care only about the keys listed in the FIELDS array. My array looks like:

FIELDS = ["timestamp", "unqiueID",etc...]

However, how do I do this if the key is nested? I.E. ['user']['color']?

How do I add a nested key to this array? I've tried: [user][color], ['user']['color'], 'user]['color, and none of them are right :) Many of the values I need are nested fields. How can I add a nested key to this array and still have the new = {k:data[k] for k in FIELDS if k in data} bit work?

like image 243
HectorOfTroy407 Avatar asked Feb 26 '26 13:02

HectorOfTroy407


1 Answers

A quite simple approach, could look like the following (it will not work for all possibilities - objects in lists/arrays). You just need to specify a 'format' how you want to look for nested values.

'findValue' will split the searchKey (here on dots) in the given object, if found it searches the next 'sub-key' in the following value (assuming it is an dict/object) ...

myObj = {
    "foo": "bar",
    "baz": {
        "foo": {
            "bar": True
        }
    }
}

def findValue(obj, searchKey):
    keys = searchKey.split('.')

    for i, subKey in enumerate(keys):
        if subKey in obj:
            if i == len(subKey) -1:
                return obj[subKey]
            else:
                obj = obj[subKey]
        else:
            print("Key not found: %s (%s)" % (subKey, keys))
            return None

res = findValue(myObj, 'foo')
print(res)

res = findValue(myObj, 'baz.foo.bar')
print(res)

res = findValue(myObj, 'cantFind')
print(res)

Returns:

bar
True
Key not found: cantFind (cantFind)
None
like image 74
Maurice Meyer Avatar answered Mar 01 '26 02:03

Maurice Meyer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!