Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find length of a multi-dimensional list?

How do you find the length of a multi-dimensional list?

I've come up with a way myself, but is this the only way to find the number of values in a multi-dimensional list?

multilist = [['1', '2', 'Ham', '4'], ['5', 'ABCD', 'Foo'], ['Bar', 'Lu', 'Shou']]
counter = 0
for minilist in multilist:
    for value in minilist:
        counter += 1

print(counter)

I'm pretty sure there is a much simpler way to find the length of a multi-dimensional list, but len(list) does not work, as it only gives the number of lists inside. Is there a more efficient method than this?

like image 975
Hailey Monette Avatar asked Apr 14 '13 05:04

Hailey Monette


1 Answers

How about:

sum(len(x) for x in multilist)
like image 124
mgilson Avatar answered Sep 22 '22 10:09

mgilson