[Tutor] "if clause" in list comprehensions.
Emile van Sebille
emile at fenx.com
Mon Oct 19 21:58:47 CEST 2009
On 10/19/2009 12:20 PM Alan Gauld said...
>
> "Sander Sweers" <sander.sweers at gmail.com> 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 thinking of isinstance() which can be used instead
> of type(). I see you use hasattr as a means of testing for a method but
> that is still different from testing type - the method names might be
> the same but the functions be completely different in effect!
>
>>> returned this: ['JOHN', 'CANADA', 'RIGHT']
>>> I was expecting this: ['JOHN', 'CANADA', 25, 32, 'RIGHT']
>>> So, actually the "if" acted like a filter.
>
> It is intended to be used as a filter.
>
>>> In order to use a list comprehension I created this function instead.
>>> def upperfy(item)
>>> try:
>>> item = item.upper()
>>> except AttributeError:
>>> pass
>>> return item
>
>> I would move return item under the except and remove the pass, other
>> might disagree on this.
>
> I would :-)
> Doing that would result in None being returned for each successful
> conversion. The OPs code is correct (even if unnecessary)
>
>>> a = [upperfy(item) for item in mylist]
>
> 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 ]
Emile
>
> HTH,
>
>
More information about the Tutor
mailing list