Imagine I have a list L that has the following items:
L = [1,1,2,3]
Let's say that I want to remove each item but also count the amount of "ones", "twos" and "threes" that are on the list. So we define the following variables
ones = 0
twos = 0
threes = 0
One way I tried to solve this problem was by using a for loop and a while loop in the following way:
for item in L:
while item in L:
if item == 1:
ones += 1
L.remove(item)
elif item == 2:
twos += 1
L.remove(item)
elif item == 3:
threes += 1
L.remove(item)
But for whatever reason, the list L remains as
L = [2]
and the variables
ones = 2
twos = 0
threes = 1
Can someone explain why is it not removing and counting the 2 and how to fix it?
Thank you in advance.
P.S. I am aware of a way of counting items on a list (count method for example) but I would still like to know why this is happening