[Tutor] "if clause" in list comprehensions.

Alan Gauld alan.gauld at btinternet.com
Tue Oct 20 00:22:26 CEST 2009


"Emile van Sebille" <emile at fenx.com> 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 strings.

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/



More information about the Tutor mailing list