Answers for "Traverse a Nested List Recursively"

0

Traverse a Nested List Recursively

def count_leaf_items(item_list):
    """Recursively counts and returns the
       number of leaf items in a (potentially
       nested) list.
    """
    count = 0
    for item in item_list:
        if isinstance(item, list):
            count += count_leaf_items(item)
        else:
            count += 1

    return count
Posted by: Guest on September-22-2021

Browse Popular Code Answers by Language