Re: Converting a flat list to a list of tuples

2005-11-24 Thread Bengt Richter
On Wed, 23 Nov 2005 13:23:21 +0100, "Fredrik Lundh" <[EMAIL PROTECTED]> wrote: >Bengt Richter wrote: > >> Are you thinking of something like lines from a file, where there might be >> chunky buffering? ISTM that wouldn't matter if the same next method was >> called. >> Here we have multiple refer

Re: Converting a flat list to a list of tuples

2005-11-23 Thread Peter Otten
Fredrik Lundh wrote: > Bengt Richter wrote: > >> Are you thinking of something like lines from a file, where there might >> be chunky buffering? ISTM that wouldn't matter if the same next method >> was called. Here we have multiple references to the same iterator. Isn't >> e.g. buiding a plain tu

Re: Converting a flat list to a list of tuples

2005-11-23 Thread [EMAIL PROTECTED]
Bengt Richter wrote: > >it's not only the order that matters, but also the number of items > >read from the source iterators on each iteration. > > > Not sure I understand. > > Are you thinking of something like lines from a file, where there might be > chunky buffering? ISTM that wouldn't matter

Re: Converting a flat list to a list of tuples

2005-11-23 Thread Simon Brunning
On 22/11/05, Bengt Richter <[EMAIL PROTECTED]> wrote: > That would be a counter-intuitive thing to do. Most things go left->right > in order as the default assumption. +1 -- Cheers, Simon B, [EMAIL PROTECTED], http://www.brunningonline.net/simon/blog/ -- http://mail.python.org/mailman/listinfo/p

Re: Converting a flat list to a list of tuples

2005-11-23 Thread Fredrik Lundh
Bengt Richter wrote: > Are you thinking of something like lines from a file, where there might be > chunky buffering? ISTM that wouldn't matter if the same next method was > called. > Here we have multiple references to the same iterator. Isn't e.g. buiding > a plain tuple defined with evaluation

Re: Converting a flat list to a list of tuples

2005-11-23 Thread Bengt Richter
On Wed, 23 Nov 2005 09:54:46 +0100, "Fredrik Lundh" <[EMAIL PROTECTED]> wrote: >Bengt Richter wrote: > >> >Though it looks nice, it's an implementation dependant solution. What if >> >someone changes zip to fetch the second item first? >> >> That would be a counter-intuitive thing to do. Most thin

Re: Converting a flat list to a list of tuples

2005-11-23 Thread Duncan Booth
Bengt Richter wrote: > On Tue, 22 Nov 2005 13:26:45 +0100, "Fredrik Lundh" > <[EMAIL PROTECTED]> wrote: >>Duncan Booth wrote: >> >>> >>> it = iter(aList) >>> >>> zip(it, it) >>> [('a', 1), ('b', 2), ('c', 3)] >> >>is "relying on undefined behaviour" perhaps the new black ? > Is it really undefin

Re: Converting a flat list to a list of tuples

2005-11-23 Thread Peter Otten
[EMAIL PROTECTED] wrote: > Personally, I would like to see it as [('a',1,'b',2), ('c',3, > None,None)],  as a list of tuple of equal length is easier to be dealt > with. > > i = iter(aList) > zip(i,chain(i,repeat(None)), > chain(i,repeat(None)),chain(i,repeat(None))) Here's some more: >>> it =

Re: Converting a flat list to a list of tuples

2005-11-23 Thread Fredrik Lundh
Bengt Richter wrote: > >Though it looks nice, it's an implementation dependant solution. What if > >someone changes zip to fetch the second item first? > > That would be a counter-intuitive thing to do. Most things go left->right > in order as the default assumption. it's not only the order that

Re: Converting a flat list to a list of tuples

2005-11-22 Thread [EMAIL PROTECTED]
Bengt Richter wrote: > On 22 Nov 2005 16:32:25 -0800, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote: > > > > >Bengt Richter wrote: > >> On 22 Nov 2005 07:42:31 -0800, "George Sakkis" <[EMAIL PROTECTED]> wrote: > >> > >> >"Laurent Rahuel" wrote: > >> > > >> >> Hi, > >> >> > >> >> newList = zip(aLis

Re: Converting a flat list to a list of tuples

2005-11-22 Thread Bengt Richter
On 22 Nov 2005 16:32:25 -0800, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote: > >Bengt Richter wrote: >> On 22 Nov 2005 07:42:31 -0800, "George Sakkis" <[EMAIL PROTECTED]> wrote: >> >> >"Laurent Rahuel" wrote: >> > >> >> Hi, >> >> >> >> newList = zip(aList[::2], aList[1::2]) >> >> newList >> >> [(

Re: Converting a flat list to a list of tuples

2005-11-22 Thread [EMAIL PROTECTED]
Bengt Richter wrote: > On 22 Nov 2005 07:42:31 -0800, "George Sakkis" <[EMAIL PROTECTED]> wrote: > > >"Laurent Rahuel" wrote: > > > >> Hi, > >> > >> newList = zip(aList[::2], aList[1::2]) > >> newList > >> [('a', 1), ('b', 2), ('c', 3)] > >> > >> Regards, > >> > >> Laurent > > > >Or if aList can g

Re: Converting a flat list to a list of tuples

2005-11-22 Thread [EMAIL PROTECTED]
Bengt Richter wrote: > On Tue, 22 Nov 2005 13:37:06 +0100, =?ISO-8859-1?Q?Andr=E9?= Malo <[EMAIL > PROTECTED]> wrote: > > >* Duncan Booth <[EMAIL PROTECTED]> wrote: > > > >> metiu uitem wrote: > >> > >> > Say you have a flat list: > >> > ['a', 1, 'b', 2, 'c', 3] > >> > > >> > How do you efficient

Re: Converting a flat list to a list of tuples

2005-11-22 Thread Bengt Richter
On 22 Nov 2005 07:42:31 -0800, "George Sakkis" <[EMAIL PROTECTED]> wrote: >"Laurent Rahuel" wrote: > >> Hi, >> >> newList = zip(aList[::2], aList[1::2]) >> newList >> [('a', 1), ('b', 2), ('c', 3)] >> >> Regards, >> >> Laurent > >Or if aList can get very large and/or the conversion has to be >perf

Re: Converting a flat list to a list of tuples

2005-11-22 Thread Dave Hansen
On Tue, 22 Nov 2005 20:12:52 GMT in comp.lang.python, [EMAIL PROTECTED] (Bengt Richter) wrote: >On Tue, 22 Nov 2005 13:26:45 +0100, "Fredrik Lundh" <[EMAIL PROTECTED]> wrote: > >>Duncan Booth wrote: >> >>> That's funny, I thought your subject line said 'list of tuples'. I'll >>> answer the questio

Re: Converting a flat list to a list of tuples

2005-11-22 Thread Bengt Richter
On Tue, 22 Nov 2005 14:28:56 GMT, "David Isaac" <[EMAIL PROTECTED]> wrote: > >"Duncan Booth" <[EMAIL PROTECTED]> wrote in message >news:[EMAIL PROTECTED] >> >>> aList = ['a', 1, 'b', 2, 'c', 3] >> >>> it = iter(aList) >> >>> zip(it, it) >> [('a', 1), ('b', 2), ('c', 3)] > >That behavior is current

Re: Converting a flat list to a list of tuples

2005-11-22 Thread Bengt Richter
On Tue, 22 Nov 2005 13:37:06 +0100, =?ISO-8859-1?Q?Andr=E9?= Malo <[EMAIL PROTECTED]> wrote: >* Duncan Booth <[EMAIL PROTECTED]> wrote: > >> metiu uitem wrote: >> >> > Say you have a flat list: >> > ['a', 1, 'b', 2, 'c', 3] >> > >> > How do you efficiently get >> > [['a', 1], ['b', 2], ['c', 3]

Re: Converting a flat list to a list of tuples

2005-11-22 Thread Bengt Richter
On Tue, 22 Nov 2005 13:26:45 +0100, "Fredrik Lundh" <[EMAIL PROTECTED]> wrote: >Duncan Booth wrote: > >> That's funny, I thought your subject line said 'list of tuples'. I'll >> answer the question in the subject rather than the question in the body: >> >> >>> aList = ['a', 1, 'b', 2, 'c', 3] >> >

Re: Converting a flat list to a list of tuples

2005-11-22 Thread Bengt Richter
On 22 Nov 2005 11:11:23 GMT, Duncan Booth <[EMAIL PROTECTED]> wrote: >metiu uitem wrote: > >> Say you have a flat list: >> ['a', 1, 'b', 2, 'c', 3] >> >> How do you efficiently get >> [['a', 1], ['b', 2], ['c', 3]] > >That's funny, I thought your subject line said 'list of tuples'. I'll >answer

Re: Converting a flat list to a list of tuples

2005-11-22 Thread George Sakkis
"Laurent Rahuel" wrote: > Hi, > > newList = zip(aList[::2], aList[1::2]) > newList > [('a', 1), ('b', 2), ('c', 3)] > > Regards, > > Laurent Or if aList can get very large and/or the conversion has to be performed many times: from itertools import islice newList = zip(islice(aList,0,None,2), isl

Re: Converting a flat list to a list of tuples

2005-11-22 Thread David Isaac
"Duncan Booth" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > >>> aList = ['a', 1, 'b', 2, 'c', 3] > >>> it = iter(aList) > >>> zip(it, it) > [('a', 1), ('b', 2), ('c', 3)] That behavior is currently an accident. http://sourceforge.net/tracker/?group_id=5470&atid=105470&func=detail

Re: Converting a flat list to a list of tuples

2005-11-22 Thread [EMAIL PROTECTED]
André Malo wrote: > * Duncan Booth <[EMAIL PROTECTED]> wrote: > > > metiu uitem wrote: > > > > > Say you have a flat list: > > > ['a', 1, 'b', 2, 'c', 3] > > > > > > How do you efficiently get > > > [['a', 1], ['b', 2], ['c', 3]] > > > > That's funny, I thought your subject line said 'list of tupl

Re: Converting a flat list to a list of tuples

2005-11-22 Thread André Malo
* Duncan Booth <[EMAIL PROTECTED]> wrote: > metiu uitem wrote: > > > Say you have a flat list: > > ['a', 1, 'b', 2, 'c', 3] > > > > How do you efficiently get > > [['a', 1], ['b', 2], ['c', 3]] > > That's funny, I thought your subject line said 'list of tuples'. I'll > answer the question in t

Re: Converting a flat list to a list of tuples

2005-11-22 Thread Fredrik Lundh
Duncan Booth wrote: > That's funny, I thought your subject line said 'list of tuples'. I'll > answer the question in the subject rather than the question in the body: > > >>> aList = ['a', 1, 'b', 2, 'c', 3] > >>> it = iter(aList) > >>> zip(it, it) > [('a', 1), ('b', 2), ('c', 3)] yesterday, we g

Re: Converting a flat list to a list of tuples

2005-11-22 Thread Steven D'Aprano
On Tue, 22 Nov 2005 11:11:23 +, Duncan Booth wrote: aList = ['a', 1, 'b', 2, 'c', 3] it = iter(aList) zip(it, it) > [('a', 1), ('b', 2), ('c', 3)] I'm not sure if I should fall to my knees in admiration of a Cool Hack, or recoil in horror at a Bogus Kludge :-) The code looks l

Re: Converting a flat list to a list of tuples

2005-11-22 Thread metiu uitem
Thanks for the answer... yes the example was wrong! -- http://mail.python.org/mailman/listinfo/python-list

Re: Converting a flat list to a list of tuples

2005-11-22 Thread Laurent Rahuel
metiu uitem wrote: > Say you have a flat list: > ['a', 1, 'b', 2, 'c', 3] > > How do you efficiently get > [['a', 1], ['b', 2], ['c', 3]] > > I was thinking of something along the lines of: > for (key,number) in list: > print key, number > > but it's not working... > > Thank you Hi, newLis

Re: Converting a flat list to a list of tuples

2005-11-22 Thread [EMAIL PROTECTED]
Duncan Booth wrote: > metiu uitem wrote: > > > Say you have a flat list: > > ['a', 1, 'b', 2, 'c', 3] > > > > How do you efficiently get > > [['a', 1], ['b', 2], ['c', 3]] > > That's funny, I thought your subject line said 'list of tuples'. I'll > answer the question in the subject rather than the

Re: Converting a flat list to a list of tuples

2005-11-22 Thread Fredrik Lundh
"metiu uitem" wrote: > Say you have a flat list: > ['a', 1, 'b', 2, 'c', 3] > > How do you efficiently get > [['a', 1], ['b', 2], ['c', 3]] simplest possible (works in all Python versions): L = ['a', 1, 'b', 2, 'c', 3] out = [] for i in range(0, len(L), 2): out.append(L[i:i+

Re: Converting a flat list to a list of tuples

2005-11-22 Thread Steven D'Aprano
On Tue, 22 Nov 2005 02:57:14 -0800, metiu uitem wrote: > Say you have a flat list: > ['a', 1, 'b', 2, 'c', 3] > > How do you efficiently get > [['a', 1], ['b', 2], ['c', 3]] def split_and_combine(L): newL = [] for i in range(len(L)//2): newL.append( [L[2*i], L[2*i+1]] ) retur

Re: Converting a flat list to a list of tuples

2005-11-22 Thread Duncan Booth
metiu uitem wrote: > Say you have a flat list: > ['a', 1, 'b', 2, 'c', 3] > > How do you efficiently get > [['a', 1], ['b', 2], ['c', 3]] That's funny, I thought your subject line said 'list of tuples'. I'll answer the question in the subject rather than the question in the body: >>> aList = [

Converting a flat list to a list of tuples

2005-11-22 Thread metiu uitem
Say you have a flat list: ['a', 1, 'b', 2, 'c', 3] How do you efficiently get [['a', 1], ['b', 2], ['c', 3]] I was thinking of something along the lines of: for (key,number) in list: print key, number but it's not working... Thank you -- http://mail.python.org/mailman/listinfo/python-list