Monday, November 18, 2024
Google search engine
HomeLanguagesPython program to find sum of elements in list

Python program to find sum of elements in list

Given a list of numbers, write a Python program to find the sum of all the elements in the list.

Example:  

Input: [12, 15, 3, 10]
Output: 40

Input: [17, 5, 3, 5]
Output: 30

Python program to find sum of elements in list

Example #1: 

Python3




# Python program to find sum of elements in list
 
total = 0
 
# creating a list
list1 = [11, 5, 17, 18, 23]
 
# Iterate each element in list
# and add them in variable total
for ele in range(0, len(list1)):
    total = total + list1[ele]
 
# printing total value
print("Sum of all elements in given list: ", total)


Output

Sum of all elements in given list:  74

Time Complexity: O(N), Here N is the number of elements in the list.
Auxiliary Space: O(1), As constant extra space is used.

Example #2 : Using while() loop  

Python3




# Python program to find sum of elements in list
total = 0
ele = 0
 
# creating a list
list1 = [11, 5, 17, 18, 23]
 
# Iterate each element in list
# and add them in variable total
while(ele < len(list1)):
    total = total + list1[ele]
    ele += 1
     
# printing total value
print("Sum of all elements in given list: ", total)


Output

Sum of all elements in given list:  74

Time Complexity: O(N), Here N is the number of elements in the list.
Auxiliary Space: O(1), As constant extra space is used.

Example #3: Recursive way  

Python3




# Python program to find sum of all
# elements in list using recursion
 
# creating a list
list1 = [11, 5, 17, 18, 23]
 
# creating sum_list function
 
 
def sumOfList(list, size):
    if (size == 0):
        return 0
    else:
        return list[size - 1] + sumOfList(list, size - 1)
 
 
# Driver code
total = sumOfList(list1, len(list1))
 
print("Sum of all elements in given list: ", total)


Output

Sum of all elements in given list:  74

Time Complexity: O(N), Here N is the number of elements in the list.
Auxiliary Space: O(1), As constant extra space is used.

Example #4: Using sum() method  

Python3




# Python program to find sum of elements in list
 
# creating a list
list1 = [11, 5, 17, 18, 23]
 
# using sum() function
total = sum(list1)
 
# printing total value
print("Sum of all elements in given list: ", total)


Output

Sum of all elements in given list:  74

Time Complexity: O(N), Here N is the number of elements in the list.
Auxiliary Space: O(1), As constant extra space is used.

Example 5: Using add() function of operator module

First we have to import the operator module then using the add() function of operator module adding the all values in the list. 

Python3




# Python 3 program to find the sum of all elements in the
# list using add function of operator module
 
from operator import*
list1 = [12, 15, 3, 10]
result = 0
for i in list1:
  # Adding elements in the list using
  # add function of operator module
    result = add(i, result)
# printing the result
print(result)


Output

40

Time Complexity: O(N), Here N is the number of elements in the list.
Auxiliary Space: O(1), As constant extra space is used.

Method 6: Using enumerate function

Python3




list1 = [12, 15, 3, 10];s=0
for i,a in enumerate(list1):
  s+=a
print(s)


Output

40

Time Complexity: O(N), Here N is the number of elements in the list.
Auxiliary Space: O(1), As constant extra space is used.

Method 7: Using list comprehension 

Python3




list1 = [12, 15, 3, 10]
s=[i for i in list1]
print(sum(s))


Output

40

Time Complexity: O(N), Here N is the number of elements in the list.
Auxiliary Space: O(1), As constant extra space is used.

Method 8: Using lambda function

Python3




list1 = [12, 15, 3, 10]
print(sum(list(filter(lambda x: (x),list1))))


Output

40

Time Complexity: O(N), Here N is the number of elements in the list.
Auxiliary Space: O(1), As constant extra space is used.

Method: Using add operator 

Python3




import operator
list1 = [12, 15, 3, 10] ;s=0
for i in list1:
  s=s+operator.add(0,i)
print(s)


Output

40

Time Complexity: O(N), Here N is the number of elements in the list.
Auxiliary Space: O(1), As constant extra space is used.

Method : Using add()+while loop

Algorithm :

Import the operator module.
Create a list of integers and assign it to the variable lst.
Initialize a variable sum to 0 to keep track of the sum.
Initialize a variable i to 0 to use as the loop counter.
Use a while loop to traverse the list.
Inside the loop, add the value at the current index of the list to the sum using the add() method of the operator module.
Increment the loop counter i by 1 in each iteration.
Print the sum of the list elements using the print() function and a message.

Python3




# Python program to find sum of elements in the list
# Importing the operator module
import operator
 
# Initializing the values
lst = [0, 4, 1, 6, 8]
sum = 0
i = 0
 
# Traversing the list using while loop finding the sum using add()
while i < len(lst):
    sum = sum+operator.add(0, lst[i])
    i = i+1
 
# printing the sum of list elements
print('The sum of elements in the given list is:', sum)
 
# This code is contributed by SHAIK HUSNA


Output

The sum of elements in the given list is: 19

Time Complexity : O(n)

Auxiliary Space : O(1)

RELATED ARTICLES

Most Popular

Recent Comments