def get_accounts(root, list=None): if list is None: list = [] for child in root.get_children(): account = Account(instance=child) if not account.get_children(): list.append(account) get_accounts(account, list) return list