We can use the try-except-else
block inside the list comprehension to handle exceptions. Here is an example:
main.py135 chars6 lines
Output:
main.py74 chars2 lines
In the above code, we use the isinstance()
function to check if the element is an integer and not zero. If it is a zero or a non-integer element, we return None
in the list comprehension.
gistlibby LogSnag