Do this as a list comprehension?

Terry Reedy tjreedy at
Sat Jun 7 05:33:24 CEST 2008

"Mensanator" <mensanator at> wrote in message 
news:c55356a4-04a0-442e-ad84-f35156cdec9c at
On Jun 6, 1:44 am, "Terry Reedy" <tjre... at> wrote:
> "Mensanator" <mensana... at> wrote in message
> news:bbd90051-36be-4378-9a27-2a47a5471d12 at
> | On Jun 5, 10:42?pm, John Salerno <johnj... at> wrote:
> | > Is it possible to write a list comprehension for this so as to 
> produce
> a
> | > list of two-item tuples?
> | >
> | > base_scores = range(8, 19)
> | > score_costs = [0, 1, 1, 1, 1, 1, 1, 2, 2, 3, 3]
> | > print zip(base_scores, score_costs)
> | >
> | > I can't think of how the structure of the list comprehension would 
> work
> | > in this case, because it seems to require iteration over two separate
> | > sequences to produce each item in the tuple.
> Which is exactly the purpose of zip, or its specialization enumerate!

Aren't you overlooking the fact that zip() truncates the output
to the shorter length iterable?
<message does not quote correctly>
<me> No.
And since the OP foolishly
hardcoded his range bounds, zip(base_scores,score_cost) will
silently return the wrong answer if the base_count list grows.
<me> So, to future proof his code he should better use 
zip(itertools.count(8), score_costs).  I consider this better than using 
enumerate to make the wrong pairing (with itertools.count(0)) and then 
correcting the mistake.
Surely enumerate() wasn't added to Python with no intention of
ever being used.
<me> Of course not, so why suggest that is was?
However, it was intended for the most common case when one wants to pair 
items with counts beginning with 0.
> Of course, enumerate(iterable) is just a facade over 
> zip(itertools.count(),
> iterable)

But if all I'm using itertools for is the count() function, why would
I go to the trouble of importing it when I can simply use enumerate()?
<me>I have no idea.  The purpose of enumerate is to be easy.
But it is not so easy when it gives the wrong pairings.
Is it a couple orders of magnitude faster?
<me> Perhaps you do not understand 'facade' - the front part or face of 
something that you see.  I was saying that enumerate is a face on a room 
containing zip and itertools.count, or the equivalent code thereof. 
Therefore, enumerate is an easy way to do a particular zip, not an 
alternative to zip.  And there should be no significant performance 
difference, certainly for long sequences which make the additional lookups 


More information about the Python-list mailing list