This is not a bug. See Remove items from a list while iterating on ways to doing this perfectly.
A list is a mutable data type. If you delete elements from it, the length (and possibly indexes of elements) of the list will change. In your second example, were you to print the length of content before and after the removal, you would see the difference, because you are not assuming the old indexes and lengths to be valid for the list.
Consider the example below to understand more on when this exception happens:
>>> content = range(4)
>>> content
[0, 1, 2, 3]
>>> len(content)
4
>>> count = 0
>>> length = len(content)
>>> while count < length:
... print count, length, content, len(content)
... content.remove(content[count])
... count += 1
...
0 4 [0, 1, 2, 3] 4
1 4 [1, 2, 3] 3
2 4 [1, 3] 2
Traceback (most recent call last):
File "<stdin>", line 3, in <module>
IndexError: list index out of range
It is clear that the length you are iterating on is a constant, but the value of count is simply corresponding to an index position which doesn't exist in the list anymore.
To work around this mutable nature of a list, you can remove elements from the end of the list:
>>> content = range(4)
>>> count = len(content) - 1
>>> while count >= 0:
... print count, length, content, len(content)
... content.remove(content[count])
... count -= 1
...
3 4 [0, 1, 2, 3] 4
2 4 [0, 1, 2] 3
1 4 [0, 1] 2
0 4 [0] 1
>>> print content
[]
Or you can pop
/ remove
the first element always, as the other answers point.