Sum 2D array in Python using map() function

In this tutorial, we are going to find the sum of a 2D array using map function in Python.

The map function takes two arguments i.e., function and iterable. It passes every element of the iterable to the function and stores the result in map object. We can covert the map object into an iterable.

Let's see how to find the sum of the 2D array using the map function.

  • Initialize the 2D array using lists.

  • Pass the function sum and 2D array to the map function.

  • Find the sum of resultant map object and print it.


See the code below.

 Live Demo

# initializing the 2D array
array = [
   [1, 2, 3],
   [4, 5, 6],
   [7, 8, 9]
# passing the sum, array to function
result = list(map(sum, array))
# see the result values
# it contains sum of every sub array


If you run the above code, you will get the following output.

[6, 15, 24]


Now, find the sum of the result using the same sum function.

# finding the sum of result


If you add the above code snippet the above program and run it, you will get the following output.



If you have any doubts in the tutorial, mention them in the comment section.