Do this as a list comprehension?

Mensanator mensanator at aol.com
Sat Jun 7 03:00:06 CEST 2008


On Jun 6, 3:19 pm, "John Salerno" <johnj... at NOSPAMgmail.com> wrote:
> "Mensanator" <mensana... at aol.com> wrote in message
>
> news:c55356a4-04a0-442e-ad84-f35156cdec9c at z72g2000hsb.googlegroups.com...
> On Jun 6, 1:44 am, "Terry Reedy" <tjre... at udel.edu> wrote:
>
> > "Mensanator" <mensana... at aol.com> wrote in message
>
> And since the OP foolishly
> hardcoded his range bounds
>
> Hmm, I just love the arrogance of some people. I actually posted a response
> to my own thread that asked about this situation of how best to make the
> range, but it doesn't seem to have posted.

It wasn't meant to be arrogant. Just that you must be careful
with zip() because it will not throw an exception if the two
iterables are of different length (this behaviour is by design)
but simply return tuples for the shorter of the iterables.

Hardcoding the range bounds instead of setting them dynamically
is a classic cause of this type of error. Obviously, you want the
range to start with 8, but what should be the upper bound?
The start plus the length of the other iterable keeping in mind
that if length is 11, last index is 8+10 since counting starts at 0.

So you want

range(8,8+len(score_costs))

Using enumerate() means you don't have to figure this out and
you'll never get an error or bad results that don't make an
error.



More information about the Python-list mailing list