python map exception continue mapping execution

you could catch the exception in your function (instead of in the for loop) and return None (or whatever you choose) if ZeroDivisionError is raised:

def one_divide_by(n):
    try:
        return 1/n
    except ZeroDivisionError:
        return None

if you choose to return None you need to adapt your format string; None can not be formatted with %f.

other values you could return (and that would be compatible with your string formatting) are float('inf') (or float('-inf') depending on the sign of your numerator) or float('nan') - "infinity" or "not a number".

here you will find some of the caveats of using float('inf').


You can move the try/except block inside the function. Example -

def one_divide_by(n):
    try:
        return 1/n
    except ZeroDivisionError:
        return 0   #or some other default value.

And then call this normally, without a try/except block -

for number, divide in zip(number_list, map(one_divide_by, number_list)):
    print("%d : %f" % (number, divide))