I frequently encounter a problem where I need to apply a function to a large iterator of data, but that function sometimes raises a known error that I want to ignore. Unfortunately, neither list compressions nor the map function has a good way to handle errors.
What is the best way to skip/deal with errors quickly in python?
For example, say I have a list of data and a function, the function raises a ValueError
whenever the data is a str. I want it to skip these values. One way to do this would be:
result = []
for n in data:
try: result.append(function(n))
except ValueError: pass
You could also do the same thing without the error checking like:
result = [function(n) for n in data]
or
result = list(map(function, data))
I want an c-compiled approach to accomplishing the above. Something in the spirit of
result = list(map(function, data, skip_errors=True))
The feature of default=value
would also be useful, so that raised errors create a default value.
I'm thinking this might be something I need to write a Cython extension for.
Note: one solution would be for me to write the catch
function I wrote in this answer in c or cython. Then I could use it in list compressions and get the performance boost I want.