Last active
October 27, 2023 14:49
-
-
Save chris-vecchio/324914fcb33307b484e2a3a9dba4e1c9 to your computer and use it in GitHub Desktop.
How to recursively search a Python dictionary for a key or list of keys. The function also searches inside tuples and lists in the dict. Also, a helper function to determine which keys are NOT present in the dictionary. Yes there are likely easier or cleaner ways to do this, but this works for me. :)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def search_keys_recursive(dictionary, keys_to_find): | |
""" | |
Recursively search for one or more keys within a dictionary (nested or one-level). | |
Args: | |
dictionary (dict): The dictionary to search. | |
keys_to_find (str or list): The key or list of keys to search for. | |
Returns: | |
list: A list of keys found within the dictionary. | |
""" | |
results = [] | |
def search_dict(d, key): | |
# Recursively search a dictionary for a key | |
for k, v in d.items(): | |
if k == key: | |
results.append(k) # Add the key to results | |
if isinstance(v, (dict, list, tuple)): | |
search(v, key) | |
def search_list(lst, key): | |
# Recursively search a list for a key | |
for item in lst: | |
if isinstance(item, (dict, list, tuple)): | |
search(item, key) | |
def search(data, key): | |
# Start search based on data type (dict, list, or tuple) | |
if isinstance(data, dict): | |
search_dict(data, key) | |
elif isinstance(data, list): | |
search_list(data, key) | |
elif isinstance(data, tuple): | |
for item in data: | |
if isinstance(item, (dict, list, tuple)): | |
search(item, key) | |
if isinstance(keys_to_find, str): | |
# Convert a single key to a list for consistent processing | |
keys_to_find = [keys_to_find] | |
for key in keys_to_find: | |
search(dictionary, key) | |
return results | |
def find_missing_keys(dictionary, keys_to_find): | |
""" | |
Find keys that are NOT present in a dictionary (nested or one-level). | |
Args: | |
dictionary (dict): The dictionary to search. | |
keys_to_find (str or list): The key or list of keys to search for. | |
Returns: | |
list: A list of missing keys not found in the dictionary. | |
""" | |
if isinstance(keys_to_find, str): | |
# Convert a single key to a list for consistent processing | |
keys_to_find = [keys_to_find] | |
found_keys = search_keys_recursive(dictionary, keys_to_find) | |
missing_keys = [key for key in keys_to_find if key not in found_keys] | |
# Return a list of missing keys | |
return missing_keys | |
# Example dict to search | |
data = { | |
'a': 42, | |
'b': { | |
'ba': 23, | |
'bb': { | |
'bba': '420' | |
} | |
}, | |
'c': [ | |
{ | |
'f': 'test', | |
'k': [ | |
( | |
[ | |
{ | |
'm': 'testm' | |
} | |
] | |
) | |
] | |
}, | |
{ | |
'g': 'test' | |
} | |
], | |
'd': ( | |
{ | |
'h': 'testh' | |
}, | |
[ | |
{ | |
'i': 'testi' | |
} | |
] | |
) | |
} | |
# List of keys to search for. Note that x and y are not in the dict | |
keys_to_find = ['a', 'ba', 'g', 'h', 'm', 'x', 'y'] | |
found_keys = search_keys_recursive(data, keys_to_find) | |
print("Found keys:", found_keys) | |
# Found keys: ['a', 'ba', 'g', 'h', 'm'] | |
missing_keys = find_missing_keys(data, keys_to_find) | |
print("Missing keys:", missing_keys) | |
# Missing keys: ['x', 'y'] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment