[Tutor] Tweaking list comprehensions
Kent Johnson
kent37 at tds.net
Mon Feb 14 02:24:02 CET 2005
Liam Clarke wrote:
> Hello,
>
> I am fine tuning list comprehensions (at least my understandng
> thereof), and I'm not near a Python interpreter at the moment, so I
> was wondering if someone could tell me if I did OK -
>
> def approachA(files):
>
> isHTML = []
> for filename in files:
> if filename.endswith('.htm') or filename.endswith('.html'):
> isHTML.append(filename)
> return isHTML
>
> def approachB(files):
>
> isHTML = [filename if filename.endswith('.htm') or\
> filename.endswith(.html') for filename in files]
> return isHTML
Looks good to me.
Kent
>
> I wanted approachB to be the list comprehension verstion of approachA.
> Did I get the syntax right? All seems somewhat back to front to me,
> although when read the comprehension does sorta make sense in a
> twisted way.
>
More information about the Tutor
mailing list