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 (Gullu) 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 Python map() function The map() function is used to apply a given function to every item of an iterable, such as a list or tuple, and returns a map object (which is an iterator). Let's start with a simple example of using map() to convert a list of strings into a list of integers.Pythons = ['1', '2', '3', '4'] res = map( 4 min read Python PySpark sum() Function PySpark, the Python API for Apache Spark, is a powerful tool for big data processing and analytics. One of its essential functions is sum(), which is part of the pyspark.sql.functions module. This function allows us to compute the sum of a column's values in a DataFrame, enabling efficient data anal 3 min read Numpy MaskedArray.sum() function | Python numpy.MaskedArray.median() function is used to compute the sum of the masked array elements over the given axis. Syntax : numpy.ma.sum(arr, axis=None, dtype=None, out=None, keepdims=False) Parameters: arr : [ ndarray ] Input masked array. axis :[ int, optional] Axis along which the sum is computed. 3 min read numpy.sum() in Python This function returns the sum of array elements over the specified axis.Syntax: numpy.sum(arr, axis, dtype, out): Parameters: arr: Input array. axis: The axis along which we want to calculate the sum value. Otherwise, it will consider arr to be flattened(works on all the axes). axis = 0 means along 3 min read Calculate the sum of all columns in a 2D NumPy array Let us see how to calculate the sum of all the columns in a 2D NumPy array.Method 1 : Using a nested loop to access the array elements column-wise and then storing their sum in a variable and then printing it.Example 1:Â Â Python3 # importing required libraries import numpy # explicit function to com 3 min read Like