- Iterate over the root keys of the dictionary
categories
, where each key represents a category. - For every key, get its value object, which represent a list of the category's subcategories, i.e. the
subcategories_list
object. - For every subcategory:
- check if its value (a string) is present in a blacklist of categories, i.e. the
category_blacklist
. - If a subcategory is present in
category_blacklist
:- Remove it from the
subcategories_list
. - Extract (i.e. pop) it's key-value pair from
categories
. - For every subcategory in its
subcategories_list
, recursively extract it and its subcategories fromcategories
.
- Remove it from the
- check if its value (a string) is present in a blacklist of categories, i.e. the
{
"category_a": ["category_b", "category_c"],
"category_b": ["category_d"],
"category_c": ["category_e"],
"category_d": [],
"category_e": []
}
{
"blacklist": ["category_b", "category_e"]
}
{
"category_a": ["category_c"],
"category_c": [],
}
If you intend to generate the full subcategory tree for a given domain (i.e. filter_in_place == False
) for a depth greater than 2 (i.e. degree > 2
), you could run into a RecursionError: maximum recursion depth exceeded
.
To potentially solve this, you will have to increase the maximum depth of recursive calls in Python with sys.setrecursionlimit(limit), e.g.:
import sys
sys.setrecursionlimit(3000)
Also, as per here take into account that limit
in sys.setrecursionlimit(limit: int)
actually refers to the max stack depth and not really recursion depth.