Looping over a list question

Fredrik Lundh fredrik at pythonware.com
Tue Oct 3 20:56:10 CEST 2006

stephen at theboulets.net wrote:

> I found myself writing:
> for f in [i for i in datafiles if '.txt' in i]:
>     print 'Processing datafile %s' % f
> but I was wishing that I could have instead written:
> for f in in datafiles if '.txt' in f:
>     print 'Processing datafile %s' % f
> Has there ever been a proposal for this?

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)


More information about the Python-list mailing list