[Tutor] "if clause" in list comprehensions.

Alan Gauld alan.gauld at btinternet.com
Tue Oct 20 00:25:37 CEST 2009


"vince spicer" <vinces1979 at gmail.com> 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
>>
>
> wrong var name "x", fixed
>


--------------------------------------------------------------------------------


> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
> 




More information about the Tutor mailing list