[prev in list] [next in list] [prev in thread] [next in thread]
List: python-list
Subject: Re: xslice idea | a generator slice
From: Ian Kelly <ian.g.kelly () gmail ! com>
Date: 2013-07-11 16:16:38
Message-ID: CALwzid=+m9jGSXVvZ0wCbhPv4sTvUKqKjEx265iaUTbOy-vb_A () mail ! gmail ! com
[Download RAW message or body]
On Thu, Jul 11, 2013 at 8:52 AM, Russel Walker <russ.pobox@gmail.com> wrote:
> Just some dribble, nothing major.
>
> I like using slices but I also noticed that a slice expression returns a new \
> sequence.
> I sometimes find myself using them in a for loop like this:
>
>
> seq = range(10)
> for even in seq[::2]:
> print even
>
>
> (That's just for an example) But wouldn't it be a bit of a waste if the slice \
> expression returns a whole new list just when all you want to do in this case is \
> iterate over it once?
> I suppose you could get around that will a little more code like:
>
>
> seq = range(10)
> for x in xrange(0, len(seq), 2):
> print seq[x]
>
>
> But it would be nice there was a way to iterate over a 'slice' of sorts, that would \
> act as a generator.
That would be the itertools.islice function.
--
http://mail.python.org/mailman/listinfo/python-list
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic