# Python Program To Find The Sum Of Elements Of An Array or List

## Problem Definition

Make a Python Function which takes an array or list as input and returns the sum of its elements, for example  array[ 1, 2, 3, 4] will return 1 + 2 + 3 + 4 = 10

## Solution

In this article, we will go through two different methods of tackling the same problem.

### Method 1: Using loop

In this method first, we are creating a variable `total` to store the sum and initializing it to 0. Next, the for loop iterates over each element of the array / list and storing their sum in the total variable, at last function returns the total.

``````def array_summer(arr):
total = 0
for item in arr:
# shorthand of total = total + item
total += item

#  Test input
print(array_summer([1, 2, 3, 3, 7]))
``````

Output:

``16``

## Method 2: Using Python's built-in function sum() [ Recommended ]

Python comes with an in-built solution for adding items of iterable (list, tuple, dictionary), the `sum()` method simply returns the sum of each item of the gives list or array.

``````def array_summer(arr):
return sum(arr)

#  Test input
print(array_summer([1, 2, 3, 3, 7]))``````

Output:

``16``

we went through two different methods of summing the elements of an array however using `sum()` function made our code more compact and faster, for a very large list `sum()` is more efficient therefore it's the recommended way.

PROGRAMS

# Latest Articles

Latest from djangocentral

## Capturing Query Parameters of request.get in Django

In Django, the request object contains a variety of information about the current HTTP request, including the query parameters. Query parameters are a way to pass additional information in the URL and are used to filter or sort data. The request object p…
{"ft_lang_label":"__label__en","ft_lang_prob":0.8151407,"math_prob":0.898181,"size":1203,"snap":"2022-40-2023-06","text_gpt3_token_len":314,"char_repetition_ratio":0.12010008,"word_repetition_ratio":0.06542056,"special_character_ratio":0.26683292,"punctuation_ratio":0.12295082,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.98012614,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-02-01T00:11:38Z\",\"WARC-Record-ID\":\"<urn:uuid:8e787eea-ea5b-49d1-805a-47eea4fa063a>\",\"Content-Length\":\"19818\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:4f9e8900-477c-487c-bf50-8fc895c097d0>\",\"WARC-Concurrent-To\":\"<urn:uuid:f1e0de86-9598-4838-83ed-8630d3d2fd11>\",\"WARC-IP-Address\":\"172.67.185.6\",\"WARC-Target-URI\":\"https://djangocentral.com/sum-of-elements-of-an-array-or-list/\",\"WARC-Payload-Digest\":\"sha1:EWQFBDBXCTADYJORDUQQIM3SOBSPTELL\",\"WARC-Block-Digest\":\"sha1:T5IQWOAQVKAJ7QMSEMNALXHOMKP5EZOG\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-06/CC-MAIN-2023-06_segments_1674764499891.42_warc_CC-MAIN-20230131222253-20230201012253-00617.warc.gz\"}"}