counting items

Mark McEahern marklists at mceahern.com
Wed Jan 12 13:23:30 EST 2005


It's me wrote:

>Okay, I give up.
>
>What's the best way to count number of items in a list [that may contain lists]?
>  
>
a = [[1,2,4],4,5,[2,3]]

def iterall(seq):
    for item in seq:
        try:
            for subitem in iterall(item):
                yield subitem
        except TypeError:
            yield item

all = [x for x in iterall(a)]
print len(all)




More information about the Python-list mailing list