I want to subtract a list of lists from corresponding elements of another list of lists such as:
a = [[1, 2], [3, 4]]
b = [[1, 2], [3, 0]]
with expected output to be:
c = [[0, 0], [0, 4]]
Subtraction with only one list from another is easy with:
c = [i - j for i, j in zip(a, b)]
but this isn't working for a list of lists and returns TypeError: unsupported operand type(s) for -: 'list' and 'list'
. Any ideas on how to do it?
This should do the job:
c = [list(map(lambda x, y: x - y, ii, jj)) for ii, jj in zip(a, b)]
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments