lyst[:None]

Raymond Hettinger vze4rx4y at verizon.net
Fri May 23 12:41:56 EDT 2003


> > My main motivation was preferring to write
> >
> >     [ foo(x) for x in bar][:limit]
>
> This makes redundant calls to foo. Everything after limit will be
> calculated and then thrown away. Try this:
>
>      [foo(x) for x in bar:[limit]]

In Python2.3, use itertools.islice():
       [foo(x) for x in islice(bar, limit)]

The iterator is very fast and avoids making a copy
of the whole sequence.  To loop to the end of the
sequence, set limit to None.


Raymond Hettinger






More information about the Python-list mailing list