From: Kevin B. <kb...@ca...> - 2001-09-12 15:53:52
|
That's cool! :-) It looks like what is happening is that the 'for i in a:' tracks the index it is working on, so if you remove items, you get every other one: >>> a = range( 10 ) >>> for i in a: ... print i ... a.remove( i ) ... 0 2 4 6 8 >>> You can make a copy of the list, and iterate over that copy: >>> a = range( 10 ) >>> for i in tuple(a): ... print i ... a.remove( i ) ... 0 1 2 3 4 5 6 7 8 9 >>> a [] >>> kb Kevin McNamee wrote: > > Hi, > > why does the following not work: > > a = [1, 2] > for i in a: > a.remove(i) > > The loop exits after the first iteration leaving a = [2] > > How do I get around this problem? > > Any help appreciated, > Kevin > > _________________________________________________ > Name/Title : Kevin McNamee, Software Consultant > Phone : +46 13 32 1165 > > _______________________________________________ > Jython-users mailing list > Jyt...@li... > https://lists.sourceforge.net/lists/listinfo/jython-users |