28 Aug 12:16
filter in for loop
From: GHZ <geraint.williams <at> gmail.com>
Subject: filter in for loop
Newsgroups: gmane.comp.python.general
Date: 2008-08-28 10:20:29 GMT
Subject: filter in for loop
Newsgroups: gmane.comp.python.general
Date: 2008-08-28 10:20:29 GMT
I would like to say something like:
for x in l if <expression>:
<do something>
e.g.
for filename in os.listdir(DIR) if filename[-4:] == '.xml':
<do something>
instead of having to say:
for filename in os.listdir(DIR):
if filename[-4:] == '.xml':
<do something>
or
for filename in (f for f in os.listdir(DIR) if f[-4] == '.xml'):
<do something>
is there a shortcut I'm missing?
--
http://mail.python.org/mailman/listinfo/python-list
RSS Feed