[Tutor] for loop issue

Oxymoron moron.oxy at gmail.com
Sat Oct 10 19:43:35 CEST 2009

On Sun, Oct 11, 2009 at 4:07 AM, Rich Lovely <roadierich at googlemail.com> wrote:
> for i, v in enumerate(x[:-1]):  #omitting last value in list to avoid IndexError
>   print v, x[i+1]

Thanks for the tip on enumerate, escaped me. Much like Kent's simply
using a temporary var escaped me despite having done similar things
often... never reply on a tiring Friday. On the bright side this
blunder with indexes, iterators, and lengths has made me more aware of
other contexts for using additional (zip, enumerate) facilities. I
hope the original poster learnt as much as I did from feebly
attempting to answer!

> I've got to say that of the two, I prefer the zip method: it looks
> cleaner, at least to my eyes.

It's an elegant usage :-), zipping up slices of the same list to
compare consecutive elems in it, hmm neat.

-- Kamal

> --
> Rich "Roadie Rich" Lovely
> There are 10 types of people in the world: those who know binary,
> those who do not, and those who are off by one.

There is more to life than increasing its speed.
 -- Mahatma Gandhi

More information about the Tutor mailing list