Do this as a list comprehension?

Mensanator mensanator at aol.com
Fri Jun 6 21:01:45 EDT 2008


On Jun 6, 1:40 pm, The Pythonista <n... at this.time> wrote:
> On Thu, 05 Jun 2008 23:42:07 -0400, John Salerno 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)
>
> score_costs = [(base_scores[i], score_costs[i]) for i in range (len
> (base_scores))]

What happens if your iterables aren't the same length?

>
> But, I'd rather just use zip. :-)

And with zip() you won't get an error, but it won't be correct,
either.

>
> --
> code.py: A blog about life, the universe, and Python
>
> http://pythonista.wordpress.com
> ** Posted fromhttp://www.teranews.com**




More information about the Python-list mailing list