Built with Tutify.io

    AIStudyLabs

    Sum of Elements in the List in Python

    Sum of Elements in the List in Python

    How to find the sum of elements in the list in Python? To find the sum of elements in a list in Python, you can use the built-in sum() function. There are different ways to calculate the sum of all the number elements in the list.

    Below are the methods to calculate the sum of elements from the list in Python.

    <a id="h-1-quick-examples-of-getting-sum-of-elements-in-a-list"></a>

    1. Quick Examples of Getting Sum of Elements in a List

    If you are in a hurry, below are some quick examples of how to find the sum of elements in the list.

    
    # Quick Examples of getting sum of elements in list
    
    # Consider the list of integers
    mylist1=[20,40,32,6,78,90]
    
    # Using sum()
    print("SUM: ", sum(mylist1))
    
    # Using sum() start from 50
    print("SUM after 50: ", sum(mylist1,50))
    
    # Using List Comprehension with sum()
    print("SUM: ", sum([i for i in mylist1]))
    
    # Using for loop with range
    total=0
    for i in range(len(mylist1)):
        total=total+mylist1[i]
    print("SUM: ", total)
    
    # Using for loop
    total=0
    for i in mylist1:
        total=total+i
    print("SUM: ", total)
    
    # Using add() with for loop
    from operator import add
    total=0
    for i in mylist1:
        total = add(i, total)
    print("SUM: ", total)
    
    # Using while loop
    total=0
    i=0
    while (i < len(mylist1)):
        total=total+mylist1[i]
        i=i+1
    print("SUM: ", total)
    

    <a id="h-2-sum-of-list-elements-in-python-using-sum"></a>

    2. Sum of List Elements in Python Using sum()

    The sum() is the built-in function in Python that is used to get or find the sum of all numeric elements from list. By using this you can also specify an initial value to start the calculating sum.

    Related: Calculate the Average of Elements in Python List

    It takes two parameters as input and returns the total sum of elements in the list. The first parameter is the input list and the second parameter specifies the start value in which the sum is computed from this value. This parameter is optional.

    <a id="h-4-1-syntax-of-sum"></a>

    4.1 Syntax of sum()

    Let's see the syntax of sum() function.

    
    sum(mylist1,start)
    
    # Using List Comprehension with sum()
    sum([iterator for iterator  in mylist1]
    
    # Using List Comprehension with sum()
    sum(list(filter(lambda iterator : (iterator ),mylist1)))
    

    <a id="h-4-2-sum-parameters"></a>

    4.2 sum() Parameters

    <a id="h-4-3-python-list-sum-examples"></a>

    4.3 Python List sum() Examples

    Let's have a list of integers and return the sum of all elements in the list using the Python sum() function. Here, we didn't specify the second parameter. So it used all elements from the list to calculate the sum and the total sum is 266.

    
    # Consider the list of integers
    mylist1=[20,40,32,6,78,90]
    
    # Calculate the sum()
    sum_of_elements = sum(mylist1)
    print("Sum of elements:", sum_of_elements)
    

    Yields below output.

    Let's have a list of integers and return the sum of all elements by specifying the initial value.

    Here, we specified the start as 10. So, initially the sum = 10, and then it adds each element to get the sum value 266. The final sum is 10+266=276.

    And, in the second example, we specified the start as 50. So, initially, the sum = 50, and the sum of elements in the list gets 266. The final sum is 50+266=316.

    
    # Consider the list of integers
    mylist1=[20,40,32,6,78,90]
    
    # Using sum() start from 10
    print("SUM after 10: ", sum(mylist1,10))
    
    # Using sum() start from 50
    print("SUM after 50: ", sum(mylist1,50))
    
    # Output:
    # SUM after 10:  276
    # SUM after 50:  316
    

    3. Using List Comprehension to get Sum of List Elements

    Let's have a list of integers and return the sum of all elements in the list using Python sum() by passing List Comprehension as a parameter. With Python list comprehension, we can create lists by specifying the elements. We select elements that we want to include, along with any conditions or operations. All this is done in a single line of code. The below example calculates the sum of numbers from the list using comprehension.

    
    # Consider the list of integers
    mylist1=[20,40,32,6,78,90]
    
    # Using List Comprehension with sum()
    print("SUM: ", sum([i for i in mylist1]))
    
    # Output:
    # SUM:  266
    

    We provided for loop inside the list comprehension and iterating each element in the list. So the sum() function will take each value from the list and return the total sum.

    <a id="h-3-find-list-sum-using-for-loop"></a>

    3. Find List Sum Using For Loop

    The for loop in Python is a control flow statement that is used to execute code repeatedly over a sequence like a string, list, tuple, set, range, or dictionary(dict) type.

    <a id="h-3-1-for-loop-with-range"></a>

    3.1 For Loop with Range

    Here, we will use range() function in for loop to iterate elements in the Python list to get a sum of elements. Inside loop, we will access each element using index position and add each element to the total variable. The sum is stored in this variable. if you wanted to start the initial value, then assign the value you wanted to the total variable.

    
    # Consider the list of integers
    mylist1=[20,40,32,6,78,90]
    
    # Using for loop with range
    total=0
    for i in range(len(mylist1)):
        total=total+mylist1[i]
    print("SUM: ", total)
    
    # Output:
    # SUM:  266
    

    <a id="h-3-2-for-loop"></a>

    3.2 For Loop

    Here, we just use the list without range() function. Here, we will get a value from the list for each iteration and add each element to the variable. The sum is stored in this variable.

    
    # Consider the list of integers
    mylist1=[20,40,32,6,78,90]
    
    # Using for loop
    total=0
    for i in mylist1:
        total=total+i
    print("SUM: ", total)
    
    # Output:
    # SUM:  266
    

    <a id="h-3-3-for-loop-with-add"></a>

    3.3 For Loop with add()

    The add() method is available in the operator module which will add elements from the list to get a sum of elements. It takes two variables as parameters.

    So we can specify this method and pass the iterator as the first parameter and total variable as the second parameter. On iteration second parameter will update and store in result again.

    
    from operator import add
    
    # Consider the list of integers
    mylist1=[20,40,32,6,78,90]
    
    # Using add() with for loop
    total=0
    for i in mylist1:
        total = add(i, total)
    print("SUM: ", total)
    
    # Output:
    # SUM:  266
    

    <a id="h-4-find-list-sum-using-while-loop"></a>

    4. Find List Sum Using While Loop

    Inside the while loop, we will access each element using the index position and add each element to the total variable. The sum is stored in this variable. Each time we need to increment the iterator till it is less than the length of the list.

    Let's create a list of integers and return the total sum using a while loop.

    
    # Consider the list of integers
    mylist1=[20,40,32,6,78,90]
    
    # Using while loop
    total=0
    i=0
    while (i < len(mylist1)):
        total=total+mylist1[i]
        i=i+1
    print("SUM: ", total)
    
    # Output:
    # SUM:  266
    

    <a id="h-frequently-asked-questions-sum-of-elements-in-the-list-in-python"></a>

    Frequently Asked Questions Sum of Elements in the List in Python

    To find the sum of elements in a list in Python, you can use the built-in sum() function. Simply pass your list as an argument to the sum() function. For example, sum(my_list) calculates the sum of all elements in the list my_list. You can replace my_list with your own list of numbers.

    The sum() function in Python is designed to work with iterable objects, such as lists. While it is primarily intended for numeric types like integers and floats, it can work with other numeric types as well. However, it won't work correctly with non-numeric types, and attempting to sum a list with non-numeric elements may result in a TypeError.

    You can find the sum of elements in a list using a loop. You can iterate over the elements of the list and accumulate the sum in a variable. Here's an example using a for loop:

    The sum() function is optimized and performs well with large lists. It's generally more efficient than manually looping through the list.

    You can find the sum of specific elements in a list by using slicing. Slicing allows you to create a new list that includes only the elements you're interested in, and then you can use the sum() function on that subset.

    <a id="h-conclusion"></a>

    Conclusion

    In this article, you have learned different ways to find the sum of list elements in Python. We used for loop to find the sum of all elements present in the list. Next, we saw how to use a while loop to find the sum of elements in the list. After that, we used sum() method to return the total sum of elements directly in the list. It can also be possible to pass the list comprehension as a parameter to this function to return the total sum.

    <a id="h-related-articles"></a>

    Related Articles