# 11. count

#1

Oops, try again. Your function fails on count([4, 'foo', 'foo'],5). It returns 1 when it should return 0.

But
print count([4, 'foo', 'foo'],5)
outputs 0

``````def count(sequence,item):
sl = len(sequence)
try:
sequence.remove(item)
except:
pass
return sl-len(sequence)
print count([4, 'foo', 'foo'],5)``````

#2

One way to count a given item in a homogenous list is to destroy all values that are not the same, and count the remainder. That is what this function attempts to do, I think. Except it fails to spot duplicates. Try testing it with 'foo'.

#4

Sir could you please comment on my code, I am a beginner so what advise you give improving efficiency?!

``````def count(x,y):
count = 0
for i in x:
if y == i:
count += 1
return count``````

#6

During this course there is no real attention being given to efficiency. That comes up later once the fundamentals are practiced and understood.

#7

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