try except in list comprehension

Solutions on MaxInterview for try except in list comprehension by the best coders in the world

showing results for - "try except in list comprehension"
Gisela
29 May 2017
1# Python List Comprehension Error Handling:
2# The correct responses to the question "how to handle exceptions in a list 
3# comprehension" are all expressing part of all of this truth: 1) literally, 
4# i.e. lexically IN the comprehension itself, you can't; 2) practically, you 
5# delegate the job to a function or check for error prone values when that's 
6# feasible.
Lya
26 May 2019
1def catch(func, *args, handle=lambda e : None, **kwargs):  
2     try:  
3         return func(*args, **kwargs)  
4     except Exception as e:  
5         return handle(e)
6