[Tutor] question about removing items from a list

Allan Crooks allan.crooks@btinternet.com
Wed, 15 Aug 2001 01:57:01 +0100


On 14 Aug 2001, at 17:42, John Kenyon wrote:

> Can anyone explain why the following works the way it does?
> 
> >>>lista = ['a', 'b', 'c', 'd']
> >>>for item in lista:
> ...	lista.remove(item)
> ...
> >>>lista
> ['b', 'd']
> 

This is happening because of the way the for loop works.

It keeps on running until it gets to the end of the list. It keeps a 
counter (starting at 0), and increments it until it gets to the end of 
the list (or, more formally, until counter >= length of list).

So what's happening?

This is your list:

lista = ['a', 'b', 'c', 'd']

The loop starts off, providing lista[0] as item. 0 is the counter. So 
we enter the loop, with the following information:

lista = ['a', 'b', 'c', 'd']
internal_counter = 0
item = 'a'

You then remove item from lista. When the loop has finished, this 
is the state of the objects.

lista = ['b', 'c', 'd']
internal_counter = 0
item = 'a'

Now the loop increments it's internal counter and checks that it 
hasn't gone past the last element in the list. The counter is 1, and 
the length of the list is 3, so everything is still OK.

So at the beginning of the next loop, the list looks like this:
lista = ['b', 'c', 'd']
internal_counter = 1
item = 'c'

This might make things clearer. Since 'a' is gone, all the other 
elements have shifted up by one. However, the counter also 
increments, so we've skipped over 'b' and gone to 'c'.

At the beginning of the next loop, the counter is incremented to 2, 
and the list has only got two elements left, 'b' and 'd' (which are 
indexed 0 and 1 respectively). Since 2 is larger than the last index, 
it quits the loop.

So how might you want to write that code? A 'better' way would be 
like this:

>>> lista = ['a', 'b', 'c', 'd']
>>> while lista:
...     lista.pop(0)
...
'a'
'b'
'c'
'd'
>>> lista
[]

An even better way would be like this:

del lista[:]

That would delete all items within the list. Doing 'del lista' would 
simply delete the list itself.

HTH,
Allan.