A couple questions

Gerhard Häring gerhard at bigfoot.de
Wed May 15 12:43:54 EDT 2002


* Gonçalo Rodrigues <op73418 at mail.telepac.pt> [2002-05-15 17:33 +0100]:
> 2. What is the pythonic way to traverse a list while modifying it? Put
> in another way, is
> 
> for i in range(len(mylist)):
>     <change mylist here>
> 
> safe or am I bound to use a while loop with an explicit counter?

As long as the length of the list stays constant, they're one and the
same thing, arent't they? Except with a while-loop, len(mylist) is
calculated on every loop iteration, while the range is only built once.

Gerhard
-- 
mail:   gerhard <at> bigfoot <dot> de       registered Linux user #64239
web:    http://www.cs.fhm.edu/~ifw00065/    OpenPGP public key id AD24C930
public key fingerprint: 3FCC 8700 3012 0A9E B0C9  3667 814B 9CAA AD24 C930
reduce(lambda x,y:x+y,map(lambda x:chr(ord(x)^42),tuple('zS^BED\nX_FOY\x0b')))





More information about the Python-list mailing list