# 1 count function with list inside list

#1

<Below this line, add a link to the EXACT exercise that you are stuck at.>

<In what way does your code behave incorrectly? Include ALL error messages.>
My code is working fine and it is being passed but i want to know what can we do if inside the list there is one more list and we need to search inside it also…can anyone pls help…i have specified an example too

``````
def count(sequence,item):

found=0
for i in sequence:
if i==item:
found+=1
return found

print count([1,1,3,[4,1],5],1)

``````

#2

Check if i is a list, and then recursively call count. this way if the inner list also contains a list it will keep going through and pass the count all the way back out

``````def count(sequence,item):

found=0
for i in sequence:
if isinstance(i, list):
found += count(i)
elif i==item:
found+=1
return found

print count([1,1,3,[4,1],5],1)
``````

#3

There is a list method in Python that you can use for this, but you should do it the long way for practice.

so think isinstance the function that they said about

#4

No… the method the exercise is talking about not using is list.count(). And it’s mostly irrelevant anyway as they said they passed the exercise they were just asking a question about something extra past the scope of the exercise

#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.