Count. Why the output is 1?


#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?


#2

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?


#3

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


#4

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