I was going through a code and i cant quiet understand this piece of code
def filter_orgs(source):
orgs = [org for o in source["orgs"] if (org := filter_orgs(o))]
if orgs or source["available"]:
return {
"org_name": source["org_name"],
"orgs": orgs
}
Usage:
source = {
"org_name": "1",
"available": True,
"orgs": [
{
"org_name": "2",
"available": False,
"orgs": []
},
{
"org_name": "3",
"available": False,
"orgs": []
},
{
"org_name": "4",
"available": True,
"orgs": []
},
{
"org_name": "5",
"available": False,
"orgs": [
{
"org_name": "6",
"available": False,
"orgs": []
},
{
"org_name": "7",
"available": False,
"orgs": []
},
{
"org_name": "8",
"available": False,
"orgs": [
{
"org_name": "9",
"available": False,
"orgs": []
},
{
"org_name": "10",
"available": True,
"orgs": []
}
]
}
]
}
]
}
filtered_source = filter_orgs(source)
Output:
{
"org_name": "1",
"orgs": [
{
"org_name": "4",
"orgs": []
},
{
"org_name": "5",
"orgs": [
{
"org_name": "8",
"orgs": [
{
"org_name": "10",
"orgs": []
}
]
}
]
}
]
}
I am new to programming. I know its a recursive function but I cant quiet get what's happening especially orgs = [org for o in source["orgs"] if (org := filter_orgs(o))] can some one care to explain .. Please, Thank you:)