By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,799 Members | 1,356 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,799 IT Pros & Developers. It's quick & easy.

Handle Exceptions Inside List Comprehension

P: n/a
Hi All,

I am wondering if there is any way to handle exceptions inside list
comprehension. For example,

[f(x) for x in xs]

I want to skip the point if f(x) raises an exception. How can I do
that without totally removing the list comprehension?

Thanks,
Geoffrey

Oct 29 '07 #1
Share this Question
Share on Google+
1 Reply


P: n/a
beginner <zy*******@gmail.comwrites:
[f(x) for x in xs]

I want to skip the point if f(x) raises an exception. How can I do
that without totally removing the list comprehension?
def ff(xs):
for x in xs:
try: yield f(x)
except: pass

[x for x in ff(xs)] or alternatively

list(ff(xs))
Oct 29 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.