Recursively filters out empty values from nested objects and arrays.
This function removes the following empty values:
None values
- Empty strings (after trimming whitespace)
- Empty lists
- Empty dictionaries
- Lists and dictionaries that become empty after filtering their contents
def filter_empty_values(
data: T,
) -> T
Examples
from typing import TypedDict
from playwright.async_api import Page
from intuned_browser import filter_empty_values
class Params(TypedDict):
pass
async def automation(page: Page, params: Params, **_kwargs):
# Filter empty values from dictionary
result1 = filter_empty_values({"a": "", "b": "hello", "c": None})
# Output: {"b": "hello"}
print(result1)
# Filter empty values from list
result2 = filter_empty_values([1, "", None, [2, ""]])
# Output: [1, [2]]
print(result2)
# Filter nested structures
result3 = filter_empty_values({"users": [{"name": ""}, {"name": "John"}]})
# Output: {"users": [{"name": "John"}]}
print(result3)
return "All data filtered successfully"
Arguments
The data structure to filter (dict, list, or any other type)
Returns: T
Filtered data structure with empty values removed