looping throuhg a list 2 items at a time

Peter Hansen peter at engcorp.com
Wed Apr 10 21:57:34 EDT 2002


David Eppstein wrote:
> 
> In article <3CB4E282.AAD468E4 at engcorp.com>,
>  Peter Hansen <peter at engcorp.com> wrote:
> 
> > If the customer wanted to handle lists with odd numbers of items
> > he would have specified the desired behaviour.  Or he will now. :-)
> 
> I don't know, silently dropping the last item seems like it could be
> nonobvious enough to cause trouble later.  You don't think it should at
> least have a sanity check, causing more obvious breakage if things
> aren't as expected?  Something like:
> 
>     i = 0
>     while i < len(L):
>         print L[i],L[i+1]
>         i += 2
> 
> which will throw an exception rather than just ignoring the last item...

True, errors should never pass silently.  I was getting carried
away by the thought you were suggesting some kind of careful
test to handle odd-length lists in a reasonable way, rather
than just suggesting a noisy kind of failure would be best.
My bad.  Sorry.

-Peter



More information about the Python-list mailing list