[Tutor] for loop issue

Oxymoron moron.oxy at gmail.com
Fri Oct 9 14:15:13 CEST 2009


On Fri, Oct 9, 2009 at 11:02 PM, Kent Johnson <kent37 at tds.net> wrote:
> On Fri, Oct 9, 2009 at 3:54 AM, Stefan Lesicnik <stefan at lsd.co.za> wrote:
>
> You can easily keep track of the previous item by assigning it to a
> variable. For example this shows just the increasing elements of a
> sequence:
>
> In [22]: items = [0, 1, 3, 2, 8, 5, 9 ]
>
> In [23]: last = None
>
> In [24]: for item in items:
>   ....:     if last is None or item > last:
>   ....:         print item
>   ....:     last = item

Darn... this is what happens when you're stuck on one solution
(referring to my index-only ways in the last post) - you miss other
obvious ways, duh * 10. Apologies for the unnecessarily complicated
exposition on iterables above. :-(



-- 
There is more to life than increasing its speed.
 -- Mahatma Gandhi


More information about the Tutor mailing list