def elementWise(A, B, operation):
    """
    execute an operate element wise and return result

    A and B are lists of lists (all lists of same lengths)
    operation is a function of two arguments and one return value
    """
    return [[operation(x, y)
             for x, y in zip(rowA, rowB)]
            for rowA, rowB in zip(A, B)]