Propagate matrix zeros
Return a new rectangular matrix where any row or column containing an original zero is entirely zero. Do not let newly written zeros propagate again.
matrix = [[1, 0], [2, 3]]
zero_columns = {1}
print([[0 if c in zero_columns else x for c,x in enumerate(row)] for row in matrix])Reason about the state
Record zero rows and columns from the original matrix first. Use those sets during a second pass to avoid cascading updates. Before writing code, identify what each variable means after one iteration. Walk through the example and explain why the next transition preserves that meaning. Then check the smallest permitted input and a case with repeated values. A correct answer follows the contract for every input, including the boundary cases in the checks. Keep any input mutation within the stated rules; return a fresh result when the task asks for one.
Your task
Return a new rectangular matrix where any row or column containing an original zero is entirely zero. Do not let newly written zeros propagate again.
The explanation opens here once every check passes.