Re: list comprehension question

2012-10-17 Thread rusi
On Oct 17, 10:22 am, Terry Reedy tjre...@udel.edu wrote: On 10/16/2012 9:54 PM, Kevin Anthony wrote: I've been teaching myself list comprehension, and i've run across something i'm not able to convert. list comprehensions specifically abbreviate the code that they are (essentially)

Re: list comprehension question

2012-10-17 Thread Hans Mulder
On 17/10/12 09:13:57, rusi wrote: On Oct 17, 10:22 am, Terry Reedy tjre...@udel.edu wrote: On 10/16/2012 9:54 PM, Kevin Anthony wrote: I've been teaching myself list comprehension, and i've run across something i'm not able to convert. list comprehensions specifically abbreviate the code

Re: list comprehension question

2012-10-17 Thread Dave Angel
On 10/17/2012 12:43 AM, Kevin Anthony wrote: Is it not true that list comprehension is much faster the the for loops? If it is not the correct way of doing this, i appoligize. Like i said, I'm learing list comprehension. (Please don't top-post; it ruins the ordering. In these forums, put

Re: list comprehension question

2012-10-17 Thread rusi
On Oct 17, 5:33 pm, Dave Angel d...@davea.name wrote: On 10/17/2012 12:43 AM, Kevin Anthony wrote: Is it not true that list comprehension is much faster the the for loops? If it is not the correct way of doing this, i appoligize. Like i said, I'm learing list comprehension. list

Re: list comprehension question

2012-10-17 Thread rusi
On Oct 17, 7:06 pm, rusi rustompm...@gmail.com wrote: On Oct 17, 5:33 pm, Dave Angel d...@davea.name wrote: On 10/17/2012 12:43 AM, Kevin Anthony wrote: Is it not true that list comprehension is much faster the the for loops? If it is not the correct way of doing this, i appoligize.

Re: list comprehension question

2012-10-17 Thread Dave Angel
On 10/17/2012 10:06 AM, rusi wrote: On Oct 17, 5:33 pm, Dave Angel d...@davea.name wrote: On 10/17/2012 12:43 AM, Kevin Anthony wrote: Is it not true that list comprehension is much faster the the for loops? If it is not the correct way of doing this, i appoligize. Like i said, I'm learing

Re: list comprehension question

2012-10-17 Thread 88888 Dihedral
Dave Angel於 2012年10月17日星期三UTC+8下午10時37分01秒寫道: On 10/17/2012 10:06 AM, rusi wrote: On Oct 17, 5:33 pm, Dave Angel d...@davea.name wrote: On 10/17/2012 12:43 AM, Kevin Anthony wrote: Is it not true that list comprehension is much faster the the for loops? If it is not the correct

Re: list comprehension question

2012-10-17 Thread rusi
On Oct 17, 7:37 pm, Dave Angel d...@davea.name wrote: And I'd wager all the improvement is in the inner loop, the dot() function. Sorry -- red herring! Changing def mm1(a,b): return [[sum(x*y for x,y in zip(ra,rb)) for rb in zip(*b)] for ra in a] to def mm1(a,b): return [[sum([x*y for x,y

Re: list comprehension question

2012-10-17 Thread 88888 Dihedral
rusi於 2012年10月17日星期三UTC+8下午10時50分11秒寫道: On Oct 17, 7:37 pm, Dave Angel d...@davea.name wrote: And I'd wager all the improvement is in the inner loop, the dot() function. Sorry -- red herring! Changing def mm1(a,b): return [[sum(x*y for x,y in zip(ra,rb)) for rb in

Re: list comprehension question

2012-10-17 Thread Terry Reedy
On 10/17/2012 3:13 AM, rusi wrote: On Oct 17, 10:22 am, Terry Reedy tjre...@udel.edu wrote: On 10/16/2012 9:54 PM, Kevin Anthony wrote: I've been teaching myself list comprehension, and i've run across something i'm not able to convert. My response is to the part Kevin could *not* convert,

Re: list comprehension question

2012-10-17 Thread Oscar Benjamin
On 17 October 2012 06:09, Dwight Hutto dwightdhu...@gmail.com wrote: On Wed, Oct 17, 2012 at 12:43 AM, Kevin Anthony kevin.s.anth...@gmail.com wrote: Is it not true that list comprehension is much faster the the for loops? If it is not the correct way of doing this, i appoligize. Like i

list comprehension question

2012-10-16 Thread Kevin Anthony
I've been teaching myself list comprehension, and i've run across something i'm not able to convert. here's the original code for matrix multiplcation retmatrix = Matrix(self.__row,other.__col) for m in range(0,retmatrix.__row): for n in range(0,retmatrix.__col): product = 0

Re: list comprehension question

2012-10-16 Thread Dave Angel
On 10/16/2012 09:54 PM, Kevin Anthony wrote: I've been teaching myself list comprehension, and i've run across something i'm not able to convert. here's the original code for matrix multiplcation retmatrix = Matrix(self.__row,other.__col) for m in range(0,retmatrix.__row): for n in

Re: list comprehension question

2012-10-16 Thread Dwight Hutto
On Tue, Oct 16, 2012 at 10:13 PM, Dwight Hutto dwightdhu...@gmail.com wrote: On Tue, Oct 16, 2012 at 9:54 PM, Kevin Anthony kevin.s.anth...@gmail.com wrote: I've been teaching myself list comprehension, and i've run across something i'm not able to convert. here's the original code for

Re: list comprehension question

2012-10-16 Thread Dwight Hutto
On Tue, Oct 16, 2012 at 10:13 PM, Dave Angel d...@davea.name wrote: On 10/16/2012 09:54 PM, Kevin Anthony wrote: I've been teaching myself list comprehension, and i've run across something i'm not able to convert. here's the original code for matrix multiplcation retmatrix =

Re: list comprehension question

2012-10-16 Thread rusi
On Oct 17, 7:14 am, Dave Angel d...@davea.name wrote: On 10/16/2012 09:54 PM, Kevin Anthony wrote: I've been teaching myself list comprehension, and i've run across something i'm not able to convert. here's the original code for matrix multiplcation retmatrix =

Re: list comprehension question

2012-10-16 Thread Kevin Anthony
Is it not true that list comprehension is much faster the the for loops? If it is not the correct way of doing this, i appoligize. Like i said, I'm learing list comprehension. Thanks Kevin On Oct 16, 2012 10:14 PM, Dave Angel d...@davea.name wrote: On 10/16/2012 09:54 PM, Kevin Anthony wrote:

Re: list comprehension question

2012-10-16 Thread Dwight Hutto
On Wed, Oct 17, 2012 at 12:43 AM, Kevin Anthony kevin.s.anth...@gmail.com wrote: Is it not true that list comprehension is much faster the the for loops? If it is not the correct way of doing this, i appoligize. Like i said, I'm learing list comprehension. I thought it was matrix

Re: list comprehension question

2012-10-16 Thread Terry Reedy
On 10/16/2012 9:54 PM, Kevin Anthony wrote: I've been teaching myself list comprehension, and i've run across something i'm not able to convert. list comprehensions specifically abbreviate the code that they are (essentially) equivalent to. res = [] for item in source: res.append(f(item))

list comprehension question

2012-02-29 Thread Johann Spies
I understand the following: In [79]: instansie instansie Out[79]: 'Mangosuthu Technikon' In [80]: t = [x.alt_name for x in lys] t = [x.alt_name for x in lys] In [81]: t t Out[81]: [] In [82]: t.append(instansie) t.append(instansie) In [83]: t t Out[83]: ['Mangosuthu Technikon'] But then why

Re: list comprehension question

2012-02-29 Thread James Broadhead
On 29 February 2012 13:52, Johann Spies johann.sp...@gmail.com wrote: In [82]: t.append(instansie) t.append(instansie) In [83]: t t Out[83]: ['Mangosuthu Technikon'] In [84]: t = [x.alt_name for x in lys].append(instansie) t = [x.alt_name for x in lys].append(instansie) In [85]: t t

Re: list comprehension question

2012-02-29 Thread John Gordon
In mailman.298.1330534919.3037.python-l...@python.org James Broadhead jamesbroadh...@gmail.com writes: On 29 February 2012 13:52, Johann Spies johann.sp...@gmail.com wrote: In [82]: t.append(instansie) t.append(instansie) In [83]: t t Out[83]: ['Mangosuthu Technikon'] In [84]: t

Re: list comprehension question

2012-02-29 Thread Chris Rebert
On Wed, Feb 29, 2012 at 5:52 AM, Johann Spies johann.sp...@gmail.com wrote: I understand the following: In [79]: instansie instansie Out[79]: 'Mangosuthu Technikon' In [80]: t = [x.alt_name for x in lys] t = [x.alt_name for x in lys] In [81]: t t Out[81]: [] In [82]:

Re: list comprehension question

2012-02-29 Thread Terry Reedy
On 2/29/2012 8:52 AM, Johann Spies wrote: Please post plain text, the standard for all python.org mailing lists and corresponding newsgroups, and not html. Some readers print the html as plain text, which is confusing and obnoxious. Other like mine, do skip the plain text version and print

Re: list comprehension question

2009-05-11 Thread J Kenneth King
Steven D'Aprano st...@remove-this-cybersource.com.au writes: On Thu, 07 May 2009 13:28:10 -0400, J Kenneth King wrote: Steven D'Aprano ste...@remove.this.cybersource.com.au writes: On Wed, 06 May 2009 09:48:51 -0400, J Kenneth King wrote: Emile van Sebille em...@fenx.com writes: On

Re: list comprehension question

2009-05-08 Thread J Kenneth King
Terry Reedy tjre...@udel.edu writes: J Kenneth King wrote: Keep in mind that nested comprehensions are still available because they do have a use case that justifies their existence. Nested comprehensions are available because because the syntax makes them available by default and making a

Re: list comprehension question

2009-05-08 Thread Steven D'Aprano
On Thu, 07 May 2009 13:28:10 -0400, J Kenneth King wrote: Steven D'Aprano ste...@remove.this.cybersource.com.au writes: On Wed, 06 May 2009 09:48:51 -0400, J Kenneth King wrote: Emile van Sebille em...@fenx.com writes: On 5/5/2009 9:15 AM J Kenneth King said... List comprehensions can

Re: list comprehension question

2009-05-07 Thread Lie Ryan
Steven D'Aprano wrote: If you’ve got the stomach for it, list comprehensions can be nested. They are a powerful tool but – like all powerful tools – they need to be used carefully, if at all. How does this discourage the use of list comprehensions? At most, it warns that complicated list

Re: list comprehension question

2009-05-07 Thread Lie Ryan
Lie Ryan wrote: Steven D'Aprano wrote: If you’ve got the stomach for it, list comprehensions can be nested. They are a powerful tool but – like all powerful tools – they need to be used carefully, if at all. How does this discourage the use of list comprehensions? At most, it warns that

Re: list comprehension question

2009-05-07 Thread Lie Ryan
Scott David Daniels wrote: John Posner wrote: Shane Geiger wrote: if type(el) == list or type(el) is tuple: A tiny improvement: if type(el) in (list, tuple): or (even better) if isinstance(el, (list, tuple)) However, it is my contention that you shouldn't be flattening by

Re: list comprehension question

2009-05-07 Thread J Kenneth King
Steven D'Aprano ste...@remove.this.cybersource.com.au writes: On Wed, 06 May 2009 09:48:51 -0400, J Kenneth King wrote: Emile van Sebille em...@fenx.com writes: On 5/5/2009 9:15 AM J Kenneth King said... List comprehensions can make a reader of your code apprehensive because it can read

Re: list comprehension question

2009-05-07 Thread Terry Reedy
J Kenneth King wrote: Keep in mind that nested comprehensions are still available because they do have a use case that justifies their existence. Nested comprehensions are available because because the syntax makes them available by default and making a fiddly exception would be contrary to

Re: list comprehension question

2009-05-06 Thread alex23
On May 6, 2:10 pm, Steven D'Aprano ste...@remove.this.cybersource.com.au wrote: It's precisely the indentation and colons (plus newlines) that makes nested for-loops easier to read than list-comps with multiple fors. You can get back *nearly* all the readability by splitting the list comp

Re: list comprehension question

2009-05-06 Thread Boris Borcic
Ross wrote: If I have a list of tuples a = [(1,2), (3,4), (5,6)], and I want to return a new list of each individual element in these tuples, I can do it with a nested for loop but when I try to do it using the list comprehension b = [j for j in i for i in a], my output is b = [5,5,5,6,6,6]

Re: list comprehension question

2009-05-06 Thread John Yeung
On May 5, 11:36 pm, alex23 wuwe...@gmail.com wrote: Apart from the presence of 'item' at the beginning of the list comprehension as opposed to 'b.append(item)' at the end of the for-loop, how exactly does the listcomp force you to bounce [..] back and forth to follow the logic? It's

Re: list comprehension question

2009-05-06 Thread J Kenneth King
Emile van Sebille em...@fenx.com writes: On 5/5/2009 9:15 AM J Kenneth King said... List comprehensions can make a reader of your code apprehensive because it can read like a run-on sentence and thus be difficult to parse. The Python documentation discourages their use and I believe for

Re: list comprehension question

2009-05-06 Thread Roel Schroeven
John Yeung schreef: Essentially, if you see [B A] over and over and over again, when finally confronted with LCs of more elements, it's not immediately clear that the pattern of increasing nestedness is [B A] = [C A B] = [D A B C] = etc. rather than [B A] = [C B A] = [D C B A] =

Re: list comprehension question

2009-05-06 Thread Emile van Sebille
On 5/6/2009 6:48 AM J Kenneth King said... Emile van Sebille em...@fenx.com writes: On 5/5/2009 9:15 AM J Kenneth King said... The Python documentation discourages their use and I believe for good reason. Can you provide a link for this? I'd like to see specifically what's being

Re: list comprehension question

2009-05-06 Thread Steven D'Aprano
On Wed, 06 May 2009 09:48:51 -0400, J Kenneth King wrote: Emile van Sebille em...@fenx.com writes: On 5/5/2009 9:15 AM J Kenneth King said... List comprehensions can make a reader of your code apprehensive because it can read like a run-on sentence and thus be difficult to parse. The

Re: list comprehension question

2009-05-05 Thread J Kenneth King
Emile van Sebille em...@fenx.com writes: On 5/1/2009 7:31 AM J Kenneth King said... Chris Rebert c...@rebertia.com writes: b = [] for pair in a: for item in pair: b.append(item) This is much more clear than a nested comprehension. I love comprehensions, but abusing them can

Re: list comprehension question

2009-05-05 Thread Emile van Sebille
On 5/5/2009 9:15 AM J Kenneth King said... List comprehensions can make a reader of your code apprehensive because it can read like a run-on sentence and thus be difficult to parse. The Python documentation discourages their use and I believe for good reason. Can you provide a link for this?

Re: Re: list comprehension question

2009-05-05 Thread J. Cliff Dyer
On Fri, 2009-05-01 at 13:00 -0400, John Posner wrote: Shane Geiger wrote: if type(el) == list or type(el) is tuple: A tiny improvement: if type(el) in (list, tuple): Another alternative, which might be useful in some cases: if hasattr(el, '__iter__'): This covers

Re: list comprehension question

2009-05-05 Thread J. Cliff Dyer
On Tue, 2009-05-05 at 12:15 -0400, J Kenneth King wrote: Emile van Sebille em...@fenx.com writes: On 5/1/2009 7:31 AM J Kenneth King said... Chris Rebert c...@rebertia.com writes: b = [] for pair in a: for item in pair: b.append(item) This is much more clear than a

Re: yet another list comprehension question

2009-05-05 Thread Ricardo Aráoz
namekuseijin wrote: On May 4, 9:15 am, David Robinow drobi...@gmail.com wrote: On Mon, May 4, 2009 at 2:33 AM, namekuseijin namekuseijin.nos...@gmail.com wrote: ls = [(1,2), (3,4), (5, None), (6,7), (8, None)] [(x,y) for (x,y) in ls if y] [(1, 2), (3, 4), (6, 7)]

Re: list comprehension question

2009-05-05 Thread Steven D'Aprano
On Tue, 05 May 2009 13:43:32 -0400, J. Cliff Dyer wrote: On Fri, 2009-05-01 at 13:00 -0400, John Posner wrote: Shane Geiger wrote: if type(el) == list or type(el) is tuple: A tiny improvement: if type(el) in (list, tuple): Another alternative, which might be useful in some

Re: yet another list comprehension question

2009-05-05 Thread namekuseijin
2009/5/5 Ricardo Aráoz ricar...@gmail.com: This seems to work for any length tuples : a = [(1,2), (3,4, 'goes'), (5,None), (6,7, 8, 'as', None), (8, None), (9, 0)] [tup for tup in a if not [e for e in tup if e == None]] [(1, 2), (3, 4, 'goes'), (9, 0)] Why that extra for? KISS a =

Re: list comprehension question

2009-05-05 Thread alex23
On May 6, 4:01 am, J. Cliff Dyer j...@sdf.lonestar.org wrote: The way you have to bounce your eyes back and forth in the comprehension makes it hard to read the logic.  With the loop, on the other hand it is blatantly obvious which way the nesting occurs. [ item for j in a if len(j)==2 for

Re: list comprehension question

2009-05-05 Thread Steven D'Aprano
On Tue, 05 May 2009 20:36:37 -0700, alex23 wrote: On May 6, 4:01 am, J. Cliff Dyer j...@sdf.lonestar.org wrote: The way you have to bounce your eyes back and forth in the comprehension makes it hard to read the logic.  With the loop, on the other hand it is blatantly obvious which way the

Re: list comprehension question

2009-05-05 Thread Terry Reedy
Steven D'Aprano wrote: On Tue, 05 May 2009 13:43:32 -0400, J. Cliff Dyer wrote: Except for the ones that it doesn't cover, like strings: hasattr('abcd', '__iter__') False True in Python3 -- http://mail.python.org/mailman/listinfo/python-list

Re: yet another list comprehension question

2009-05-04 Thread namekuseijin
ls = [(1,2), (3,4), (5, None), (6,7), (8, None)] [(x,y) for (x,y) in ls if y] [(1, 2), (3, 4), (6, 7)] -- http://mail.python.org/mailman/listinfo/python-list

Re: yet another list comprehension question

2009-05-04 Thread Arnaud Delobelle
Snorri H a.a.ovtchinni...@gmail.com writes: On May 3, 6:13 am, Ross ross.j...@gmail.com wrote: I'm trying to set up a simple filter using a list comprehension. If I have a list of tuples, a = [(1,2), (3,4), (5,None), (6,7), (8, None)] and I wanted to filter out all tuples containing None, I

Re: yet another list comprehension question

2009-05-04 Thread Snorri H
On May 3, 6:13 am, Ross ross.j...@gmail.com wrote: I'm trying to set up a simple filter using a list comprehension. If I have a list of tuples, a = [(1,2), (3,4), (5,None), (6,7), (8, None)] and I wanted to filter out all tuples containing None, I would like to get the new list b = [(1,2),

Re: yet another list comprehension question

2009-05-04 Thread David Robinow
On Mon, May 4, 2009 at 2:33 AM, namekuseijin namekuseijin.nos...@gmail.com wrote: ls = [(1,2), (3,4), (5, None), (6,7), (8, None)] [(x,y) for (x,y) in ls if y] [(1, 2), (3, 4), (6, 7)] Nope. That filters out 0 as well as None. Not what the OP asked for. --

Re: yet another list comprehension question

2009-05-04 Thread namekuseijin
On May 4, 9:15 am, David Robinow drobi...@gmail.com wrote: On Mon, May 4, 2009 at 2:33 AM, namekuseijin namekuseijin.nos...@gmail.com wrote: ls = [(1,2), (3,4), (5, None), (6,7), (8, None)] [(x,y) for (x,y) in ls if y] [(1, 2), (3, 4), (6, 7)] Nope. That filters out 0 as well as None.

Re: yet another list comprehension question

2009-05-03 Thread ma
This isn't list comprehension, but it's something to keep in mind: b = filter(lambda x: None not in x, input_list) On Sat, May 2, 2009 at 10:25 PM, CTO debat...@gmail.com wrote: On May 2, 10:13 pm, Ross ross.j...@gmail.com wrote: I'm trying to set up a simple filter using a list

yet another list comprehension question

2009-05-02 Thread Ross
I'm trying to set up a simple filter using a list comprehension. If I have a list of tuples, a = [(1,2), (3,4), (5,None), (6,7), (8, None)] and I wanted to filter out all tuples containing None, I would like to get the new list b = [(1,2), (3,4),(6,7)]. I tried b = [i for i in a if t for t in i

Re: yet another list comprehension question

2009-05-02 Thread Chris Rebert
On Sat, May 2, 2009 at 7:13 PM, Ross ross.j...@gmail.com wrote: I'm trying to set up a simple filter using a list comprehension. If I have a list of tuples, a = [(1,2), (3,4), (5,None), (6,7), (8, None)] and I wanted to filter out all tuples containing None, I would like to get the new list b

Re: yet another list comprehension question

2009-05-02 Thread Ross
On May 2, 7:21 pm, Chris Rebert c...@rebertia.com wrote: On Sat, May 2, 2009 at 7:13 PM, Ross ross.j...@gmail.com wrote: I'm trying to set up a simple filter using a list comprehension. If I have a list of tuples, a = [(1,2), (3,4), (5,None), (6,7), (8, None)] and I wanted to filter out all

Re: yet another list comprehension question

2009-05-02 Thread CTO
On May 2, 10:13 pm, Ross ross.j...@gmail.com wrote: I'm trying to set up a simple filter using a list comprehension. If I have a list of tuples, a = [(1,2), (3,4), (5,None), (6,7), (8, None)] and I wanted to filter out all tuples containing None, I would like to get the new list b = [(1,2),

Re: list comprehension question

2009-05-01 Thread Arnaud Delobelle
Ross ross.j...@gmail.com writes: If I have a list of tuples a = [(1,2), (3,4), (5,6)], and I want to return a new list of each individual element in these tuples, I can do it with a nested for loop but when I try to do it using the list comprehension b = [j for j in i for i in a], my output

Re: list comprehension question

2009-05-01 Thread thor
On May 1, 2:28 pm, Arnaud Delobelle arno...@googlemail.com wrote: Ross ross.j...@gmail.com writes: If I have a list of tuples a = [(1,2), (3,4), (5,6)], and I want to return a new list of each individual element in these tuples, I can do it with a nested for loop but when I try to do it

Re: list comprehension question

2009-05-01 Thread J Kenneth King
Chris Rebert c...@rebertia.com writes: On Thu, Apr 30, 2009 at 5:56 PM, Ross ross.j...@gmail.com wrote: If I have a list of tuples a = [(1,2), (3,4), (5,6)], and I want to return a new list of each individual element in these tuples, I can do it with a nested for loop but when I try to do it

Re: list comprehension question

2009-05-01 Thread Emile van Sebille
On 5/1/2009 7:31 AM J Kenneth King said... Chris Rebert c...@rebertia.com writes: b = [] for pair in a: for item in pair: b.append(item) This is much more clear than a nested comprehension. I love comprehensions, but abusing them can lead to really dense and difficult to read

Re: list comprehension question

2009-05-01 Thread Arnaud Delobelle
Emile van Sebille em...@fenx.com writes: On 5/1/2009 7:31 AM J Kenneth King said... Chris Rebert c...@rebertia.com writes: b = [] for pair in a: for item in pair: b.append(item) This is much more clear than a nested comprehension. I love comprehensions, but abusing them can

Re: list comprehension question

2009-05-01 Thread Shane Geiger
from goopy.functional import flatten # http://sourceforge.net/projects/goog-goopy/ b = [(1,2), (3,4), (5,6)] print flatten(b) #from goopy.functional import flatten # http://sourceforge.net/projects/goog-goopy/ def flatten(seq): Returns a list of the contents of seq with sublists and

Re: Re: list comprehension question

2009-05-01 Thread John Posner
Shane Geiger wrote: if type(el) == list or type(el) is tuple: A tiny improvement: if type(el) in (list, tuple): -- http://mail.python.org/mailman/listinfo/python-list

Re: list comprehension question

2009-05-01 Thread Emile van Sebille
On 5/1/2009 9:19 AM Arnaud Delobelle said... Emile van Sebille em...@fenx.com writes: On 5/1/2009 7:31 AM J Kenneth King said... I love comprehensions, but abusing them can lead to really dense and difficult to read code. I disagree on dense and difficult, although I'll leave open the

Re: list comprehension question

2009-05-01 Thread Arnaud Delobelle
Emile van Sebille em...@fenx.com writes: On 5/1/2009 9:19 AM Arnaud Delobelle said... Emile van Sebille em...@fenx.com writes: On 5/1/2009 7:31 AM J Kenneth King said... I love comprehensions, but abusing them can lead to really dense and difficult to read code. I disagree on dense and

Re: list comprehension question

2009-05-01 Thread Scott David Daniels
John Posner wrote: Shane Geiger wrote: if type(el) == list or type(el) is tuple: A tiny improvement: if type(el) in (list, tuple): or (even better) if isinstance(el, (list, tuple)) However, it is my contention that you shouldn't be flattening by type -- you should know where,

list comprehension question

2009-04-30 Thread Ross
If I have a list of tuples a = [(1,2), (3,4), (5,6)], and I want to return a new list of each individual element in these tuples, I can do it with a nested for loop but when I try to do it using the list comprehension b = [j for j in i for i in a], my output is b = [5,5,5,6,6,6] instead of the

Re: list comprehension question

2009-04-30 Thread Chris Rebert
On Thu, Apr 30, 2009 at 5:56 PM, Ross ross.j...@gmail.com wrote: If I have a list of tuples a = [(1,2), (3,4), (5,6)], and I want to return a new list of each individual element in these tuples, I can do it with a nested for loop but when I try to do it using the list comprehension b = [j for

Re: list comprehension question

2009-04-30 Thread Michael Spencer
Ross wrote: If I have a list of tuples a = [(1,2), (3,4), (5,6)], and I want to return a new list of each individual element in these tuples, I can do it with a nested for loop but when I try to do it using the list comprehension b = [j for j in i for i in a], my output is b = [5,5,5,6,6,6]

quick beginners List comprehension question

2009-01-21 Thread Dr Mephesto
Hi, Im new to python, and OOP, and am trying to get a handle on list comprehension. Say I have a class Foo with a property called bar: class Foo: def __init__(self): self.bar = random.randint(1,100) and then I make a list of these objects: Newlist = [] for x in range(10):

Re: quick beginners List comprehension question

2009-01-21 Thread Diez B. Roggisch
Dr Mephesto wrote: Hi, Im new to python, and OOP, and am trying to get a handle on list comprehension. Say I have a class Foo with a property called bar: class Foo: def __init__(self): self.bar = random.randint(1,100) and then I make a list of these objects: Newlist =

Re: quick beginners List comprehension question

2009-01-21 Thread MRAB
Dr Mephesto wrote: Hi, Im new to python, and OOP, and am trying to get a handle on list comprehension. Say I have a class Foo with a property called bar: class Foo: def __init__(self): self.bar = random.randint(1,100) and then I make a list of these objects: Newlist = [] for x in

Re: quick beginners List comprehension question

2009-01-21 Thread Philip Semanchuk
On Jan 21, 2009, at 10:52 AM, Dr Mephesto wrote: Hi, Im new to python, and OOP, and am trying to get a handle on list comprehension. Say I have a class Foo with a property called bar: class Foo: def __init__(self): self.bar = random.randint(1,100) and then I make a list of these

Re: quick beginners List comprehension question

2009-01-21 Thread MRAB
Diez B. Roggisch wrote: Dr Mephesto wrote: Hi, Im new to python, and OOP, and am trying to get a handle on list comprehension. Say I have a class Foo with a property called bar: class Foo: def __init__(self): self.bar = random.randint(1,100) and then I make a list of these

Re: quick beginners List comprehension question

2009-01-21 Thread Lou Pecora
In article mailman.7691.1232554737.3487.python-l...@python.org, Philip Semanchuk phi...@semanchuk.com wrote: Other answers have been good; to them I'll add the comment that list comprehensions are for *constructing* lists, not manipulating the elements thereof. HTH Philip Well

Re: quick beginners List comprehension question

2009-01-21 Thread Steve Holden
Lou Pecora wrote: In article mailman.7691.1232554737.3487.python-l...@python.org, Philip Semanchuk phi...@semanchuk.com wrote: Other answers have been good; to them I'll add the comment that list comprehensions are for *constructing* lists, not manipulating the elements thereof. HTH

Re: quick beginners List comprehension question

2009-01-21 Thread Philip Semanchuk
On Jan 21, 2009, at 11:52 AM, Lou Pecora wrote: In article mailman.7691.1232554737.3487.python-l...@python.org, Philip Semanchuk phi...@semanchuk.com wrote: Other answers have been good; to them I'll add the comment that list comprehensions are for *constructing* lists, not manipulating the

Re: quick beginners List comprehension question

2009-01-21 Thread Terry Reedy
Dr Mephesto wrote: Hi, Im new to python, and OOP, and am trying to get a handle on list comprehension. Say I have a class Foo with a property called bar: class Foo: def __init__(self): self.bar = random.randint(1,100) and then I make a list of these objects: Newlist = [] for x in

Re: quick beginners List comprehension question

2009-01-21 Thread Diez B. Roggisch
MRAB schrieb: Diez B. Roggisch wrote: Dr Mephesto wrote: Hi, Im new to python, and OOP, and am trying to get a handle on list comprehension. Say I have a class Foo with a property called bar: class Foo: def __init__(self): self.bar = random.randint(1,100) and then I make a list

Re: quick beginners List comprehension question

2009-01-21 Thread Srinivasa NL
You can try this import random class foo: def __init__(self): self.bar = random.randint(1,100) def getbar(ls,i): ls.append(foo()) ls[i].bar = ls[i].bar * 3 ls = [] [getbar(ls,i) for i in range(10)] On Thu, Jan 22, 2009 at 4:45 AM, Diez B. Roggisch de...@nospam.web.dewrote:

Re: List Comprehension Question: One to Many Mapping?

2007-09-14 Thread beginner
On Aug 24, 5:35 am, Boris Borcic [EMAIL PROTECTED] wrote: Paul Rubin wrote: beginner [EMAIL PROTECTED] writes: For example, if I have x=[ [1,2], [3,4] ] What I want is a new list of list that has four sub-lists: [[1,2], [f(1), f(2)], [3,4], [f(3), f(4)]] [[a, map(f,a)] for a in x]

Re: List Comprehension Question: One to Many Mapping?

2007-08-24 Thread Paul Rubin
beginner [EMAIL PROTECTED] writes: For example, if I have x=[ [1,2], [3,4] ] What I want is a new list of list that has four sub-lists: [[1,2], [f(1), f(2)], [3,4], [f(3), f(4)]] [[a, map(f,a)] for a in x] -- http://mail.python.org/mailman/listinfo/python-list

Re: List Comprehension Question: One to Many Mapping?

2007-08-24 Thread Paul Rubin
Boris Borcic [EMAIL PROTECTED] writes: For example, if I have x=[ [1,2], [3,4] ] What I want is a new list of list that has four sub-lists: [[1,2], [f(1), f(2)], [3,4], [f(3), f(4)]] [[a, map(f,a)] for a in x] no, that one will be [[[1,2], [f(1), f(2)]], [[3,4], [f(3), f(4)]]] eg

Re: List Comprehension Question: One to Many Mapping?

2007-08-24 Thread Boris Borcic
Paul Rubin wrote: beginner [EMAIL PROTECTED] writes: For example, if I have x=[ [1,2], [3,4] ] What I want is a new list of list that has four sub-lists: [[1,2], [f(1), f(2)], [3,4], [f(3), f(4)]] [[a, map(f,a)] for a in x] no, that one will be [[[1,2], [f(1), f(2)]], [[3,4], [f(3),

Re: List Comprehension Question: One to Many Mapping?

2007-08-24 Thread beginner
On Aug 24, 12:44 am, beginner [EMAIL PROTECTED] wrote: On Aug 24, 12:41 am, Davo [EMAIL PROTECTED] wrote: On Aug 23, 9:24 pm, beginner [EMAIL PROTECTED] wrote: Hi All, How do I map a list to two lists with list comprehension? For example, if I have x=[ [1,2], [3,4] ] What I

Re: List Comprehension Question: One to Many Mapping?

2007-08-24 Thread beginner
On Aug 24, 5:47 am, Paul Rubin http://[EMAIL PROTECTED] wrote: Boris Borcic [EMAIL PROTECTED] writes: For example, if I have x=[ [1,2], [3,4] ] What I want is a new list of list that has four sub-lists: [[1,2], [f(1), f(2)], [3,4], [f(3), f(4)]] [[a, map(f,a)] for a in x] no,

Re: List Comprehension Question: One to Many Mapping?

2007-08-24 Thread ZeD
Boris Borcic wrote: For example, if I have x=[ [1,2], [3,4] ] What I want is a new list of list that has four sub-lists: [[1,2], [f(1), f(2)], [3,4], [f(3), f(4)]] [[a, map(f,a)] for a in x] [map(g,a) for a in x for g in [None,f]] will do it. ...a bit too cleverly, but there's worse :

Re: List Comprehension Question: One to Many Mapping?

2007-08-24 Thread Eduardo O. Padoan
For example, if I have x=[ [1,2], [3,4] ] What I want is a new list of list that has four sub-lists: [[1,2], [f(1), f(2)], [3,4], [f(3), f(4)]] [[a, [f(b) for b in a]] for a in x] -- http://mail.python.org/mailman/listinfo/python-list

List Comprehension Question: One to Many Mapping?

2007-08-23 Thread beginner
Hi All, How do I map a list to two lists with list comprehension? For example, if I have x=[ [1,2], [3,4] ] What I want is a new list of list that has four sub-lists: [[1,2], [f(1), f(2)], [3,4], [f(3), f(4)]] [1,2] is mapped to [1,2] and [f(1), f(2)] and [3,4] is mapped to [3,4], [f(3),

Re: List Comprehension Question: One to Many Mapping?

2007-08-23 Thread [EMAIL PROTECTED]
On Aug 23, 9:24 pm, beginner [EMAIL PROTECTED] wrote: Hi All, How do I map a list to two lists with list comprehension? For example, if I have x=[ [1,2], [3,4] ] What I want is a new list of list that has four sub-lists: [[1,2], [f(1), f(2)], [3,4], [f(3), f(4)]] [1,2] is mapped to

Re: List Comprehension Question: One to Many Mapping?

2007-08-23 Thread Peter Otten
beginner wrote: How do I map a list to two lists with list comprehension? For example, if I have x=[ [1,2], [3,4] ] What I want is a new list of list that has four sub-lists: [[1,2], [f(1), f(2)], [3,4], [f(3), f(4)]] [1,2] is mapped to [1,2] and [f(1), f(2)] and [3,4] is mapped to

Re: List Comprehension Question: One to Many Mapping?

2007-08-23 Thread beginner
On Aug 24, 12:41 am, Davo [EMAIL PROTECTED] wrote: On Aug 23, 9:24 pm, beginner [EMAIL PROTECTED] wrote: Hi All, How do I map a list to two lists with list comprehension? For example, if I have x=[ [1,2], [3,4] ] What I want is a new list of list that has four sub-lists:

Re: List Comprehension Question: One to Many Mapping?

2007-08-23 Thread Davo
On Aug 23, 9:24 pm, beginner [EMAIL PROTECTED] wrote: Hi All, How do I map a list to two lists with list comprehension? For example, if I have x=[ [1,2], [3,4] ] What I want is a new list of list that has four sub-lists: [[1,2], [f(1), f(2)], [3,4], [f(3), f(4)]] [1,2] is mapped to