Sum 2D array in Python using map() function Last Updated : 21 Jul, 2022 Comments Improve Suggest changes Like Article Like Report Given a 2-D matrix, we need to find sum of all elements present in matrix ? Examples: Input : arr = [[1, 2, 3], [4, 5, 6], [2, 1, 2]] Output : Sum = 26 This problem can be solved easily using two for loops by iterating whole matrix but we can solve this problem quickly in python using map() function. Python3 # Function to calculate sum of all elements in matrix # sum(arr) is a python inbuilt function which calculates # sum of each element in a iterable ( array, list etc ). # map(sum,arr) applies a given function to each item of # an iterable and returns a list of the results. def findSum(arr): # inner map function applies inbuilt function # sum on each row of matrix arr and returns # list of sum of elements of each row return sum(map(sum,arr)) # Driver function if __name__ == "__main__": arr = [[1, 2, 3], [4, 5, 6], [2, 1, 2]] print ("Sum = ",findSum(arr)) OutputSum = 26 What does map() do? The map() function applies a given function to each item of an iterable(list, tuple etc.) and returns a list of the results. For example see given below example : Python3 # Python code to demonstrate working of map() # Function to calculate square of any number def calculateSquare(n): return n*n # numbers is a list of elements numbers = [1, 2, 3, 4] # Here map function is mapping calculateSquare # function to each element of numbers list. # It is similar to pass each element of numbers # list one by one into calculateSquare function # and store result in another list result = map(calculateSquare, numbers) # resultant output will be [1,4,9,16] print (result) set_result=list(result) print(set_result) Output<map object at 0x7fdf95d2a6d8> [1, 4, 9, 16] Comment More infoAdvertise with us Next Article Sum 2D array in Python using map() function S Shashank Mishra Improve Article Tags : Misc Matrix Python DSA Practice Tags : MatrixMiscpython Similar Reads Prefix Sum Array in Python using Accumulate Function A prefix sum array is an array where each element is the cumulative sum of all the previous elements up to that point in the original array. For example, given an array [1, 2, 3, 4], its prefix sum array would be [1, 3, 6, 10]. Let us explore various methods to achieve this.Using accumulate from ite 3 min read Map function and Dictionary in Python to sum ASCII values We are given a sentence in the English language(which can also contain digits), and we need to compute and print the sum of ASCII values of the characters of each word in that sentence. Examples: Input : GeeksforGeeks, a computer science portal for geeksOutput : Sentence representation as sum of ASC 2 min read sum() function in Python The sum of numbers in the list is required everywhere. Python provides an inbuilt function sum() which sums up the numbers in the list. Pythonarr = [1, 5, 2] print(sum(arr))Output8 Sum() Function in Python Syntax Syntax : sum(iterable, start) iterable : iterable can be anything list , tuples or dict 3 min read Python Program to Find Sum of Array Given an array of integers, find the sum of its elements. Examples: Input : arr[] = {1, 2, 3}Output : 6Explanation: 1 + 2 + 3 = 6This Python program calculates the sum of an array by iterating through each element and adding it to a running total. The sum is then returned. An example usage is provid 4 min read How to Map a Function Over NumPy Array? Mapping a function over a NumPy array means applying a specific operation to each element individually. This lets you transform all elements of the array efficiently without writing explicit loops. For example, if you want to add 2 to every element in an array [1, 2, 3, 4, 5], the result will be [3, 2 min read Like