On Jul 29, 3:38 pm, Terry Reedy <[EMAIL PROTECTED]> wrote:
> kj wrote:
> > Is there a special pythonic idiom for iterating over a list (or
> > tuple) two elements at a time?
>
> > I mean, other than
>
> > for i in range(0, len(a), 2):
> > frobnicate(a[i], a[i+1])
>
> There have been requests to
On Jul 29, 10:36 am, kj <[EMAIL PROTECTED]> wrote:
> Is there a special pythonic idiom for iterating over a list (or
> tuple) two elements at a time?
>
> I mean, other than
>
> for i in range(0, len(a), 2):
> frobnicate(a[i], a[i+1])
>
> ?
>
> I think I once saw something like
>
> for (x, y) in
On Jul 29, 4:11 pm, Erik Max Francis <[EMAIL PROTECTED]> wrote:
> [EMAIL PROTECTED] wrote:
> > for x, y in zip(a, a[1:]):
> > frob(x, y)
>
> What you meant was this:
>
> >>> [(x, y) for x, y in zip(a[::2], a[1::2])]
> [(0, 1), (2, 3), (4, 5), (6, 7), (8, 9)]
>
> but this creates three sublists
The following method is similar to the others provided, but yields an
index value as well (similar to the enumerate(iterable) function).
This is useful in some (but not all) situations.
If the iterable object's iterator returns an even number of items then
you're fine; otherwise it will throw away
In <[EMAIL PROTECTED]> Terry Reedy <[EMAIL PROTECTED]> writes:
>kj wrote:
>> Is there a special pythonic idiom for iterating over a list (or
>> tuple) two elements at a time?
>>
>> I mean, other than
>>
>> for i in range(0, len(a), 2):
>> frobnicate(a[i], a[i+1])
>There have been request
kj wrote:
Is there a special pythonic idiom for iterating over a list (or
tuple) two elements at a time?
I mean, other than
for i in range(0, len(a), 2):
frobnicate(a[i], a[i+1])
There have been requests to add a grouper function to itertools, but its
author has resisted because there
Thanks for all the replies. I learned a lot!
kynn
--
NOTE: In my address everything before the first period is backwards;
and the last period, and everything after it, should be discarded.
--
http://mail.python.org/mailman/listinfo/python-list
[EMAIL PROTECTED] wrote:
Whoops, I misread the original post. That would be:
for x, y in zip(a[::2], a[1::2]):
frob(x, y)
... which I don't use a lot.
Sorry, posted before I saw your reply. Still, you're building three
sublists in order to just iterate over them.
--
Erik Max Fr
[EMAIL PROTECTED] wrote:
On Jul 29, 1:36 pm, kj <[EMAIL PROTECTED]> wrote:
Is there a special pythonic idiom for iterating over a list (or
tuple) two elements at a time?
I use this one a lot:
for x, y in zip(a, a[1:]):
frob(x, y)
It doesn't work:
>>> a = range(10)
>>> [(x, y) for
On Jul 29, 2:36 pm, [EMAIL PROTECTED] wrote:
> On Jul 29, 1:36 pm, kj <[EMAIL PROTECTED]> wrote:
>
> > Is there a special pythonic idiom for iterating over a list (or
> > tuple) two elements at a time?
>
> I use this one a lot:
>
> for x, y in zip(a, a[1:]):
> frob(x, y)
>
> Geoff G-T
kj <[EMAIL PROTECTED]> wrote:
> Is there a special pythonic idiom for iterating over a list (or
> tuple) two elements at a time?
I don't know of one, and I shouldn't be answering, but the following
should work:
def gulp_two(items):
for i,j in zip(items[0::2], items[1::2]):
yield (i,j)
Let'
On Jul 29, 1:36 pm, kj <[EMAIL PROTECTED]> wrote:
> Is there a special pythonic idiom for iterating over a list (or
> tuple) two elements at a time?
I use this one a lot:
for x, y in zip(a, a[1:]):
frob(x, y)
Geoff G-T
--
http://mail.python.org/mailman/listinfo/python-list
Something like this may be fast enough:
>>> from itertools import izip
>>> xpartition = lambda seq, n=2: izip(*(iter(seq),) * n)
>>> xprimes = (x for x in xrange(2, 100) if all(x % i for i in xrange(2, x)))
>>> list(xpartition(xprimes))
[(2, 3), (5, 7), (11, 13), (17, 19), (23, 29), (31, 37), (41,
kj wrote:
Is there a special pythonic idiom for iterating over a list (or
tuple) two elements at a time?
I mean, other than
for i in range(0, len(a), 2):
frobnicate(a[i], a[i+1])
?
I think I once saw something like
for (x, y) in forgotten_expression_using(a):
frobnicate(x, y)
Or may
Is there a special pythonic idiom for iterating over a list (or
tuple) two elements at a time?
I mean, other than
for i in range(0, len(a), 2):
frobnicate(a[i], a[i+1])
?
I think I once saw something like
for (x, y) in forgotten_expression_using(a):
frobnicate(x, y)
Or maybe I just
15 matches
Mail list logo