
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
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.
Example
See the code below.
# 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 print(result)
Output
If you run the above code, you will get the following output.
[6, 15, 24]
Output
Now, find the sum of the result using the same sum function.
# finding the sum of result print(sum(result))
Output
If you add the above code snippet the above program and run it, you will get the following output.
45
Conclusion
If you have any doubts in the tutorial, mention them in the comment section.
- Related Articles
- Convert 2D array to object using map or reduce in JavaScript
- Prefix sum array in python using accumulate function
- Map function and Dictionary in Python to sum ASCII values
- Prefix Sum of Matrix (Or 2D Array) in C++
- How to use my object like an array using map function in JavaScript?
- Minimum sum submatrix in a given 2D array in C++
- Passing a 2D array to a C++ function
- JavaScript: How to map array values without using "map" method?
- sum() function in Python
- Maximum length of consecutive 1’s in a binary string in Python using Map function
- How to store a 2d Array in another 2d Array in java?
- Map Sum Pairs in JavaScript
- Map function and Lambda expression in Python to replace characters
- What is the use of the map function in Python?
- Plotting Google Map using gmplot package in Python?
