Re: [Tutor] "if clause" in list comprehensions.

2009-10-21 Thread Kent Johnson
On Wed, Oct 21, 2009 at 3:30 AM, Albert-Jan Roskam wrote: > Is the if-else (esp. 'else') in a list comprehension specific for Python 3.x? > Or did I miss something? List comps have always allowed a condition since they were introduced: [ for in if ] This is the form of the original questi

Re: [Tutor] "if clause" in list comprehensions.

2009-10-21 Thread ALAN GAULD
> Is the if-else (esp. 'else') in a list comprehension specific for Python 3.x? > Or > did I miss something? Its not part of list comprehensions per se, you can use it in any *expression*. It was introduced in Python 2.5 as a response to C's ternary operator: C: x = foo?bar:baz Python x =

Re: [Tutor] "if clause" in list comprehensions.

2009-10-21 Thread Christian Witts
way when you do criticize them, you're a mile away and you have their shoes! ~~ --- On Tue, 10/20/09, Alan Gauld wrote: From: Alan Gauld Subject: Re: [Tutor] "if clause" in list comprehensions. To: tutor

Re: [Tutor] "if clause" in list comprehensions.

2009-10-21 Thread Albert-Jan Roskam
icize them, you're a mile away and you have their shoes! ~~ --- On Tue, 10/20/09, Alan Gauld wrote: > From: Alan Gauld > Subject: Re: [Tutor] "if clause" in list comprehensions. > To: tutor@python.org > D

Re: [Tutor] "if clause" in list comprehensions.

2009-10-19 Thread Alan Gauld
Ooops, hit send by mistake... "vince spicer" wrote Lambda can save the day to keep everything on one line, and leave variable type the same: mylist = ['John', 'Canada', 25, 32, 'right'] new_list = [(lambda y: y.upper() if hasattr(y, 'upper') else y)(a) for a in mylist ] >> ['JACK', 'CANA

Re: [Tutor] "if clause" in list comprehensions.

2009-10-19 Thread Alan Gauld
"vince spicer" wrote Lambda can save the day to keep everything on one line, and leave variable type the same: mylist = ['John', 'Canada', 25, 32, 'right'] new_list = [(lambda y: y.upper() if hasattr(y, 'upper') else y)(a) for a in mylist ] >> ['JACK', 'CANADA', 25, 32, 'RIGHT'] Vince

Re: [Tutor] "if clause" in list comprehensions.

2009-10-19 Thread Alan Gauld
"Emile van Sebille" wrote a = [item.upper() if type(item) == str else item for item in mylist] should do it I think. or even a = [ str(item).upper() for item in mylist ] That was my first attempt but the OP wanted his integers preserved as integers whereas this would convert them to

Re: [Tutor] "if clause" in list comprehensions.

2009-10-19 Thread ALAN GAULD
Is there any compelling reason to write: > >[item.upper() for item in my_list if isinstance(item, basestring)] > >rather than the following? > >[item.upper() for item in my_list if hasattr(item, 'upper')]What happens if >you have an object in your list that has an 'upper' merthod that, say, reboo

Re: [Tutor] "if clause" in list comprehensions.

2009-10-19 Thread Eduardo Vieira
On Mon, Oct 19, 2009 at 1:20 PM, Alan Gauld wrote: > > "Sander Sweers" wrote > >>> mylist = ['John', 'Canada', 25, 32, 'right'] >>> a = [item.upper() for item in mylist if type(item) == type('good')] >> >> Usually it is recommended to use hasattr() instead of type() >>   hasattr(s, 'upper') > > N

Re: [Tutor] "if clause" in list comprehensions.

2009-10-19 Thread Douglas Philips
On or about 2009 Oct 19, at 3:57 PM, Sander Sweers indited: I missed that the try: did not return anything. I was thinking more of something like this. def upperfy(item): try: item.upper() return item except AttributeError: return item Thanks for correcting me! Depe

Re: [Tutor] "if clause" in list comprehensions.

2009-10-19 Thread Emmanuel Ruellan
On Mon, Oct 19, 2009 at 9:20 PM, Alan Gauld wrote: > > > "Sander Sweers" wrote > > mylist = ['John', 'Canada', 25, 32, 'right'] >>> a = [item.upper() for item in mylist if type(item) == type('good')] >>> >> >> Usually it is recommended to use hasattr() instead of type() >> hasattr(s, 'upper')

Re: [Tutor] "if clause" in list comprehensions.

2009-10-19 Thread vince spicer
On Mon, Oct 19, 2009 at 2:14 PM, vince spicer wrote: > > > On Mon, Oct 19, 2009 at 1:58 PM, Emile van Sebille wrote: > >> On 10/19/2009 12:20 PM Alan Gauld said... >> >> >>> "Sander Sweers" wrote >>> >>> mylist = ['John', 'Canada', 25, 32, 'right'] > a = [item.upper() for item in mylist if

Re: [Tutor] "if clause" in list comprehensions.

2009-10-19 Thread vince spicer
On Mon, Oct 19, 2009 at 1:58 PM, Emile van Sebille wrote: > On 10/19/2009 12:20 PM Alan Gauld said... > > >> "Sander Sweers" wrote >> >> mylist = ['John', 'Canada', 25, 32, 'right'] a = [item.upper() for item in mylist if type(item) == type('good')] >>> >>> Usually it is recommended t

Re: [Tutor] "if clause" in list comprehensions.

2009-10-19 Thread Emile van Sebille
On 10/19/2009 12:20 PM Alan Gauld said... "Sander Sweers" wrote mylist = ['John', 'Canada', 25, 32, 'right'] a = [item.upper() for item in mylist if type(item) == type('good')] Usually it is recommended to use hasattr() instead of type() hasattr(s, 'upper') Nope, they do completely di

Re: [Tutor] "if clause" in list comprehensions.

2009-10-19 Thread Sander Sweers
2009/10/19 Alan Gauld : >> Usually it is recommended to use hasattr() instead of type() >>   hasattr(s, 'upper') > > Nope, they do  completely different things > I think you might be thinking of isinstance() which can be used instead of > type(). I see you use hasattr as a means of testing for a me

Re: [Tutor] "if clause" in list comprehensions.

2009-10-19 Thread Alan Gauld
"Sander Sweers" wrote mylist = ['John', 'Canada', 25, 32, 'right'] a = [item.upper() for item in mylist if type(item) == type('good')] Usually it is recommended to use hasattr() instead of type() hasattr(s, 'upper') Nope, they do completely different things I think you might be thinkin

Re: [Tutor] "if clause" in list comprehensions.

2009-10-19 Thread Wayne
On Mon, Oct 19, 2009 at 11:39 AM, Eduardo Vieira wrote: > Hello, > The other day I was making a script and decided to use a list > compreehension and I found out that this code: > mylist = ['John', 'Canada', 25, 32, 'right'] > a = [item.upper() for item in mylist if type(item) == type('good')] > r

Re: [Tutor] "if clause" in list comprehensions.

2009-10-19 Thread Sander Sweers
2009/10/19 Eduardo Vieira : > mylist = ['John', 'Canada', 25, 32, 'right'] > a = [item.upper() for item in mylist if type(item) == type('good')] Usually it is recommended to use hasattr() instead of type() hasattr(s, 'upper') > returned this: ['JOHN', 'CANADA', 'RIGHT'] > I was expecting this

[Tutor] "if clause" in list comprehensions.

2009-10-19 Thread Eduardo Vieira
Hello, The other day I was making a script and decided to use a list compreehension and I found out that this code: mylist = ['John', 'Canada', 25, 32, 'right'] a = [item.upper() for item in mylist if type(item) == type('good')] returned this: ['JOHN', 'CANADA', 'RIGHT'] I was expecting this: ['JOH