Filter key from Nested item using Python


Python can extract a specific value from a complex data structure that has nested dictionaries or lists by filtering a key from a nested item. The key is a unique identifier for a certain value inside the nested item. Based on this problem statement it has various application domains such as data manipulation, data interaction, and, API interaction.

Syntax

The following syntax is used in the examples-

append()

This is a built−in method in Python that adds the element at the end of the list.

isintance()

Python has a built−in function called isinstance() that determines whether an object is an instance or subclass of a certain class or type.

values()

The values() is a built−in function in Python that can be used to get every value from the dictionary.

pop()

The built−in method pop() in Python is used to remove or return the given index value.

Using Recursion with isintance()

In the following example, the program uses recursion means the function calls to function itself where inside the function the built−in function isintance() is used to check the object type and by returning the function it will use the dictionary comprehension.

Example

def filter_keys_nested_item(data, keys):
    if isinstance(data, dict):
        return {k: filter_keys_nested_item(v, keys) for k, v in data.items() if k in keys}
    elif isinstance(data, list):
        return [filter_keys_nested_item(item, keys) for item in data]
    else:
        return data

# create the nested item
nested_item = {
    "key1": "value1",
    "key2": {
        "key3": "value3",
        "key4": "value4"
    },
    "key5": [
        {"key6": "value6"},
        {"key7": "value7"}
    ]
}
filtered_item = filter_keys_nested_item(nested_item, ["key2", "key4", "key6"])
print(filtered_item)

Output

 {'key2': {'key4': 'value4'}}

Using a stack

In the following example, the program removes the nested items based on specific keys. The program adjusts both nested dictionaries and lists, to explore the nested items accordingly. So it traverses through the nested structure of dictionaries to check whether each key matches by given key or not. If the matches key is found then it will add the corresponding value in the filtered output. Finally, it will use the function return to filter the nested item and display the result.

Example

def filter_keys_nested_item(data, keys):
    stack = [(data, {})]
    while stack:
        current, filtered = stack.pop()
        if isinstance(current, dict):
            for k, v in current.items():
                if k in keys:
                    filtered[k] = filter_keys_nested_item(v, keys)
                elif isinstance(v, (dict, list)):
                    stack.append((v, {}))
        elif isinstance(current, list):
            for item in current:
                stack.append((item, {}))
    return filtered

# create the nested item
nested_item = {
    "key1": "value1",
    "key2": {
        "key3": "value3",
        "key4": "value4"
    },
    "key5": [
        {"key6": "value6"},
        {"key7": "value7"}
    ]
}
filtered_item = filter_keys_nested_item(nested_item, ["key2", "key4", "key5"])
print(filtered_item)

Output

 {'key2': {'key4': {}}, 'key5': {}}

Using a Recursive Generator

In the following example, the program uses a recursive generator that is similar to list comprehension that filters the nested list or dictionary based on specific keys. Using a recursive function it will mention the specific key to filter the nested item. It allows the deep traversal of nested structures to the given input, if the matches are found then it yields the corresponding filter output.

Example

def filter_keys_nested_item(data, keys):
    if isinstance(data, dict):
        filtered = {k: filter_keys_nested_item(v, keys) for k, v in data.items() if k in keys}
    elif isinstance(data, list):
        filtered = [filter_keys_nested_item(item, keys) for item in data]
    else:
        return data

    if filtered:
        yield filtered

    for item in filtered.values() if isinstance(filtered, dict) else filtered:
        if isinstance(item, dict) or isinstance(item, list):
            yield from filter_keys_nested_item(item, keys)

# create the nested item
nested_item = {
    "key1": "value1",
    "key2": {
        "key3": "value3",
        "key4": "value4"
    },
    "key5": [
        {"key6": "value6"},
        {"key7": "value7"}
    ]
}
filtered_item = next(filter_keys_nested_item(nested_item, ["key2", "key4", "key6"]))
print(filtered_item)

Output

 {'key2': <generator object filter_keys_nested_item at 0x7f29d0e4aab0>}

Conclusion

We discussed the various ways to solve the problem statement. For many applications, including data manipulation, data analysis, API interactions, and configuration parsing, filtering keys from nested objects is essential. It allows us to parse API responses, analyze subsets of data, and modify the behavior of the application based on configuration parameters.

Updated on: 14-Aug-2023

359 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements