[Tutor] iterating over a sequence question..

Luke Paireepinart rabidpoobear at gmail.com
Mon Jun 18 20:37:21 CEST 2007


On 6/18/07, Simon Hooper <simon at partex.co.uk> wrote:
>
> Hi Luke,
>
> * On 17/06/07, Luke Paireepinart wrote:
> > a more expanded version that accounts for either list being the longer
> > one, or both being the same length, would be:
> >
> >  >>> if len(t) > len(l): x = len(t)
> > else: x = len(l)
> >  >>> print [(l[i%len(l)],t[i%len(t)]) for i in range(x)]
> > [(1, 'r'), (2, 'g'), (3, 'b'), (4, 'r'), (5, 'g')]
>
> Being the duffer that I am, I'm very pleased with myself that I came up
> with a similar solution (albeit as a function rather than a list
> comprehension) :)
>
> You do not need the if statement either,


Yeah, I never knew about the max() function!
I noticed someone else used it in one of their solutions.
I'm pretty sure I've seen it a lot before, just didn't remember it.
-Luke
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20070618/1cf0ac67/attachment.html 


More information about the Tutor mailing list