# Nested List Items

 0 I am trying to find a way to count the distinct list items within any given nest level within a list. I think the sum of all of these should actually add up to "Deep Count". I have Deep Count working but I am stuck with the sub totals. I am using len() to determine the number on a given nested level but can't code the recursive piece properly(or traverse ?). I am also using the is_list function to test the items to see if they are lists. I know that for example the following list(al) should return 2 items for the first nest, representing the distinct value 'a' + the second list but I am interested in the distinct values. Any suggestions would be a big help. and would allow me to finish CS101 finally :) al = ['a', ['b', ['c','e']]] def branch_count(p): branch = [] for e in p: branch.append(len(p)) if is_list(e): for y in e: branch.append(len(p)) return branch  asked 08 Apr '12, 12:34 Paul Taylor 3.8k●1●6●28 accept rate: 8% Not sure what you mean bu "count the distinct list items within any given nest level within a list". In your example, what would the "distinct values" you are interested in be? Also noticed the return statement in your code may not be indented correctly. Shouldn't it be at the same indent as "for e in p:" in order to return branch? Of course, all the lines under your def line should be indented as well. (08 Apr '12, 15:08) malckwan Thanks, indent was just a cut and paste problem and branch was in the wrong spot. The distinct values in the example above would have been 'a' for the first nest, 'b' in the second nest and 'c' + 'e' in the last nest. Think I found another way to finish the question. (09 Apr '12, 04:10) Paul Taylor

 0 How do you know that p is a list? You might want to check that it is one before you create a list, which, if my comprehension is sufficient, is: branch= [len(p) for e in p]  (at least as long as no e is a list itself) And how come your function returns poo? (sorry, I mean that it returns as soon as it reaches p[0][0]) answered 08 Apr '12, 19:23 Christoph M 3.4k●3●19●52 Thanks, I was calling the is_list function beforehand to check that p was a list. I will check your code snippit. (09 Apr '12, 04:15) Paul Taylor
 0 ThThere are 2 mistakes. First is that if the element is not list, your procedure will not return anything. So your code need to be like this: def branch_count(p): branch = [] for e in p: # some code return branch  and the second is that you don't use recursion. But I cannot help you with the second problem till the deadline because it will provide you with the answer for the exam task. But you are close to the right code. So read again lecture about recursion. Hope you will manage with it! answered 09 Apr '12, 02:51 Stavrovietsk... 432●1●3●32
Question text:

Markdown Basics

• *italic* or _italic_
• **bold** or __bold__
• image?![alt text](/path/img.jpg "Title")
• numbered list: 1. Foo 2. Bar
• to add a line break simply add two spaces to where you would like the new line to be.
• basic HTML tags are also supported

×15,303
×665
×639
×84
×12