Looping over a list question
stephen at theboulets.net
stephen at theboulets.net
Tue Oct 3 15:52:31 EDT 2006
Fredrik Lundh wrote:
[snip]
> probably. but unless your editor or keyboard is horribly broken, you
> can of course press return at the right place instead:
>
> for i in datafiles:
> if '.txt' in i:
> print 'Processing datafile %s' % f
>
> (for this specific case, "endswith" is more reliable than "in", btw)
>
> </F>
My "print" line is actually a long 40 line block. I guess I could just
do
datafiles = [f for f in datafiles if f.endswith('.txt')]
and then loop over it.
mdsteele at gmail.com wrote:
> Yes, there has:
> http://groups.google.ca/group/comp.lang.python/browse_thread/thread/9...
Thanks for the link. I didn't know about str.startswith and
str.endswith, which I can see is very useful.
So, not a universally popular proposal. Interesting how different
programming 'phrasings' resonate with different people. For me, for
example, lambda causes my eyes to glaze over.
I do love list comprehensions though. I'd also love to see string
constants implemented some day too (like str.whitespace and
str.ascii_letters).
Stephen
More information about the Python-list
mailing list