This is shorter, but yours is more readable.

class Solution(object): def uniquePathsWithObstacles(self, M): m, n = len(M), len(M[0]) dp = [[0]*n for i in range(m)] for i in range(m): for j in range( n): if M[i][j] == 0: if i == 0 and j == 0: dp[i][j] = 1 else: dp[i][j] = (i != 0) * dp[i-1][j] + (j != 0) * dp[i][j-1] return dp[-1][-1]