View Single Post
Old May 3rd, 2008, 4:43 PM   #4
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Posts: 1,788
Rep Power: 5 Sane will become famous soon enough
Re: Are nested lists indestructable?

So if you had:

abspirlist = [['name', 'att', 'def', 1], 
              ['name', 'att', 'def', 1], 
              ['name', 'att', 'def', 1]]

You want the number 12, for the total number of nested elements?

You can do that with a little fancy work, by generating a list of lengths (in this case: [4, 4, 4]).

Then feeding it to the sum function (sum([4,4,4]) evaluates 12).

Combining these two ideas gives us:

sum((len(x) for x in abspirlist))
Sane is offline   Reply With Quote