Do this as a list comprehension?

Ricardo Aráoz ricaraoz at gmail.com
Mon Jun 9 16:06:43 CEST 2008


Mensanator wrote:
> 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.
> 

Wouldn't it be nice to have leftZip(), rightZip(), and fullZip() for 
when the lists have different lengths? The final tuples for a leftZip 
could be in the form (value, ) and for right zip (, value) (though I 
think this last tuple is not allowed in python's syntax, we might define 
a "Null" or "Empty" name to act as a place holder in the resulting tuples).




More information about the Python-list mailing list