You are summing the inner lists A, so just apply sum to each nested list in A:
def degree(A):
return map(sum, A)
You could also consider using a list comprehension or generator expression (depending on whether or not you need to produce the result lazily (map() in Python 2 produces a list, in Python 3 it works lazily):
def degree(A):
# returns a list
return [sum(row) for row in A]
def degree(A):
# returns a generator
return (sum(row) for row in A)
You can otherwise produce i and j as a cartesian product, using itertools.product(); however, you'd be passing in a tuple of integers to the map callable:
from itertools import product
map(lambda ij: A[ij[0]][ij[1]] ** 2, product(range(len(A)), 2))
This can be remedied by using itertools.starmap() instead:
from itertools import product, starmap
starmap(lambda i, j: A[i][i] ** 2, product(range(len(A)), 2))
Neither produces a sum; I demoed a lambda that produces the square of the innermost values instead, so you get one long sequence of all values, squared, without distinction between rows. That's how mapping works, it produces a value per item processed in the input.
You could also just nest the map() calls; put the inner map() in the callable for the outer map():
map(lambda r: map(lambda v: v ** 2, r), A)
but note that this then produces a lazy sequence of lazy objects in Python 3. Again, summing doesn't make sense in this scenario, as there is no accumulation of results.
For a generic cumulation of results across a sequence, you want to use the functools.reduce() function; that callable applies a callable on a running result and the next value in a sequence. You can produce a sum with your lambda x, y: x + y function and map():
map(lambda r: reduce(lambda x, y: x + y, r, 0), A)
But for just summing, the reduce(lambda x, y: x + y, <iterable>, 0) syntax is just a verbose and slower way of spelling sum(<iterable>).
map(sum, A), by the way, no need to nest anything.map()doesn't make any sense in your context; where you thinking ofreduce()perhaps?map()takes any number of iterables, and applies those together as inputs, butmap(..., A)only has one input, so one argument for the lambda. You then build a sequence of results, not a total sum.reduce()produces a single result from repeated calls to a function and the input sequence.