468,457 Members | 1,682 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,457 developers. It's quick & easy.

Handle Exceptions Inside List Comprehension

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
1 2886
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.

Similar topics

23 posts views Thread by Fuzzyman | last post: by
35 posts views Thread by Moosebumps | last post: by
6 posts views Thread by jena | last post: by
18 posts views Thread by a | last post: by
4 posts views Thread by Gregory Guthrie | last post: by
6 posts views Thread by Liming | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by subhajit12345 | last post: by
reply views Thread by kmladenovski | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.