comple list slices

johnzenger at gmail.com johnzenger at gmail.com
Tue Feb 28 11:13:20 EST 2006


Python lets you iterate through a list using an integer index, too,
although if you do so we will make fun of you.  You can accomplish it
with a while loop, as in:

i = 0
while i < len(rows):
   if rows[i] == "This code looks like BASIC without the WEND, doesn't
it?":
      rowgroups.append("Pretty much.")
   i += 1     # or i += rowspan, whatever.

Do not try to do this with a for loop.  In Python, "for i in xrange(5)"
is more like a "foreach $i ( {0,1,2,3,4,5})" in Perl, so changing i in
the loop will not change the value of i on the next loop iteration.




More information about the Python-list mailing list