Looping over a list question

Tim Williams tim at tdw.net
Tue Oct 3 14:46:40 EDT 2006


On 3 Oct 2006 10:50:04 -0700, stephen at theboulets.net
<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? Just wondering ...
>

Maybe

>>> def myfunc(txt):
... 	print txt
... 	
>>> datafiles = ['1.txt','2.txt','3.txt','4.tst']
>>> null = [myfunc(i) for i in datafiles if '.txt' in i]
1.txt
2.txt
3.txt
>>>



More information about the Python-list mailing list