HELP! when i put remove func. into for loop somethings gets wrong/Python 3.4


#1

Im dealing with codes for over 5 hours because of this.What i want to do is get rid of the "a" list in the end.
Remove all elements in it by looping.And count the same letters.For instance there are 4 "d"

a = ["A","b","c","d","E","d","B","c","d","e","d"]
count = 0
for b in a:
y = b.upper()

print(b)
for x in a:
    print(x)
    if x.upper() == y:

        count += 1
        a.remove(x)
    else:
        pass

print(a)

the result of this
['d', 'd', 'd', 'd']
but i want is
[]

Well,the problem is,when loop iterating through letters,it misses one letter sometimes.Doesnt follow the order.For instance,when we iterate this:

A
A
c - See? this should have been "b" because "c" comes after "b" Thats my real problem :frowning:
d
E
d
B
c
d
e
d
...


#2

You're iterating through the list that you're removing from, that'll affect the loop and you have to account for it.

It might be more convenient (and probably faster, removing takes time since it has to find the element, then remove it, then move all the following elements to take up the empty slot) - to create a new list with the values that you wish to keep.