Re: iterating "by twos"

2008-08-07 Thread castironpi
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

Re: iterating "by twos"

2008-08-07 Thread shahms . king
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

Re: iterating "by twos"

2008-07-30 Thread giltay
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

Re: iterating "by twos"

2008-07-29 Thread Daniel da Silva
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

Re: iterating "by twos"

2008-07-29 Thread kj
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

Re: iterating "by twos"

2008-07-29 Thread Terry Reedy
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

Re: iterating "by twos"

2008-07-29 Thread kj
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

Re: iterating "by twos"

2008-07-29 Thread Erik Max Francis
[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

Re: iterating "by twos"

2008-07-29 Thread Erik Max Francis
[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

Re: iterating "by twos"

2008-07-29 Thread giltay
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

Re: iterating "by twos"

2008-07-29 Thread william tanksley
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'

Re: iterating "by twos"

2008-07-29 Thread giltay
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

Re: iterating "by twos"

2008-07-29 Thread bearophileHUGS
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,

Re: iterating "by twos"

2008-07-29 Thread George Trojan
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

iterating "by twos"

2008-07-29 Thread kj
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