Count. Why the output is 1?

def count(sequence, item):
    count = 0
    for i in sequence:
        print i
        if sequence[i] == item:
            count += 1
    return int(count)    
print count([1,2,1,1], 1)

the output is 1, I cannot figure out why the output is 1.
Anyone could help me?


If I use for i in range(len(sequence))
the output is correct, but I want to know why "for i in sequence:" is wrong..Anyone could help please?


for i in sequence works fine, but then the comparison should be:

if i == item:

since i will hold the item itself. if you use len(), you will get the index, and then you would indeed if sequence[i] == item, in that case you use the index to access the item in the comparison


you are right, I had mess with these type of "for"