Find sum of elements in array
In this article, we will take a look at 4 different ways to find the sum of elements of an array in python.
Below are the methods covered
First method to find the sum of elements in a python array is by iterating over the array using a loop and adding up each array element. Example,
def sum_array(arr): total = 0 for num in arr: total += num return total numbers = [1, 2, 3, 4, 5] sum = sum_array(numbers) print(sum)
This program defines a function sum_array()
that takes an array as an input and finds the sum of its elements.
Inside the function, a variable total
is initialized to 0. The function then uses a for
loop to iterate over each element in the array.
For each element, it adds the value of that element to the total
variable. After the loop completes, the total
variable will hold the sum of all elements in the array.
The function is then called with an array of numbers as an argument and the result is printed.
2. enumerate() function
This method is similar to above method but it used enumerate() function.
enumerate()
accepts an iterable (list, tuple, array or string) as an input and returns an iterator that produces tuples containing the index and the corresponding element of the iterable.
It can be used with a loop to find the sum of elements of an array. Example,
def sum_array(arr): total = 0 for i, num in enumerate(arr): total += num return total numbers = [1, 2, 3, 4, 5] sum = sum_array(numbers) print(sum)
In this program, enumerate()
function is used inside the for
loop to iterate over the elements of the input array.
The loop variable i
holds the index of the current element and the loop variable num
holds the value of the current element.
The value of each element is added to the total
variable, just as in the previous example.
Python reduce() function is used to reduce the elements of an iterable to a single result.
So, it can be used to add the elements of an array.
reduce()
is applied to an iterable, reducing it to a single value. It takes two arguments:1. the function to apply, and
2. the iterable to reduce.
Below is the program example of using reduce()
to find the sum of array elements.
Note that in order to use reduce()
, we need to import functools
module.
from functools import reduce def sum_array(arr): return reduce(lambda a, b: a + b, arr) numbers = [1, 2, 3, 4, 5] sum = sum_array(numbers) print(sum)
As stated above, reduce()
accepts a function as first argument. So, we are supplying it a Lambda function.
A lambda function is a function without a name. It is similar to a normal function except that it can be defined, where required.
So, the first argument to reduce() is a lambda function. This lambda function takes two arguments, a
and b
, and returns their sum.
The second argument passed to reduce()
is the input array.
The reduce()
function starts by applying the lambda function to the first two elements of the array.
The result of this operation becomes the new value of a
, and the next element of the array becomes the new value of b
.
reduce()
continues to apply the lambda function to the new values of a
and b
, updating a
with the result and moving to the next element of the array until the end of the array.
So, the function reduce()
applies the lambda function to the elements of the array in a cumulative way, and the final result is the sum of all elements in the array.
4. Recursion
Recursion means to call a function itself repeatedly till a certain condition is met.
To find the sum of array elements using recursion, we need to define a function that takes the array and its length as arguments. Example,
def sum_array(arr, n): if n <= 0: return 0 else: return arr[n-1] + sum_array(arr, n-1) numbers = [1, 2, 3, 4, 5] n = len(numbers) sum = sum_array(numbers, n) print(sum)
In this program, the sum_array()
function is defined to take two arguments: the input array arr
, and the length of the array n
.
The function uses recursion to calculate the sum of the elements in the array.
The condition to stop the recursion is when the length of the array becomes zero or negative.
In every recursive call, the function finds the sum of the last element of the array and the sum of the rest of the elements by calling the function recursively with the same array, but with the length reduced by 1.
This way, the function will go through the array one by one and keep adding the element to the sum till the end of the array is reached.
Finally, the function is called with the input array and its length as arguments and the result is printed.
Remember that this method is not very efficient, since it calls the function multiple times with the same array, which can cause the stack to overflow for large arrays.