3

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?

Community
  • 1
  • 1
HectorOfTroy407
  • 1,737
  • 5
  • 21
  • 31

2 Answers2

1

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
Maurice Meyer
  • 17,279
  • 4
  • 30
  • 47
-1

Create a recursive function which checks whether the dictionary key has value or dictionary. If key has dictionary again call function until you find the non-dictionary value. When you find value just add it to your new created dictionary.

Hope this helps.

Jay Parikh
  • 2,419
  • 17
  • 13