We will write 8 ways to write python function to sum the numbers in a Python List. Let’s see how many different ways we can achieve this.
In the below function we initiated a variable sm as 0, iterate through the list which is passed in function as argument (lst). keep adding each number of lists in sm and at the end return sm to get the sum of all the numbers in python list.
mylist=[23,45,12,87,38]
def ListSum(lst):
sm=0
for i in mylist:
sm=sm+i
return sm
ListSum(mylist)
Use pop() function :
In the below function we used pop() function of python list to sum all the numbers in the list . Here also we initiated a variable sm as 0, iterate through the list which is passed in function as argument (lst) and pop each list number one by one and keep adding each number of lists in sm and at the end return sm to get the sum of all the numbers in python list.
mylist=[23,45,12,87,38]
def ListSum(lst):
sm=0
for i in range(len(lst)):
sm=sm+lst.pop()
return sm
ListSum(mylist)
Use sum() function:
In the below function we used sum() function function of python list to sum all the numbers in the list . In this case we just passed the list argument passed in function as input to Sum method and return it .
mylist=[23,45,12,87,38]
def ListSum(lst):
return sum(lst)
ListSum(mylist)
Use python indexing :
In the below function we used list indexing along with range() and len() function to sum all the numbers in the list. In this case we run the loop n times where n is length of the list as we have used range method so the i value will be 0 in first iteration 1 in second ,2 in third and so on . We initialize a variable sm with 0 and keep on adding each list item which we will pull during each iteration using list indexing.
mylist=[23,45,12,87,38]
def ListSum(lst):
sm=0
for i in range(len(lst)):
sm=sm+lst[i]
return sm
ListSum(mylist)
Use list unpacking using *
In the below function we used list unpacking using *. We store lst passed as an argument in two variable sm and nlst . sm will store first element of the list and rest element of the list will be stored as a list in nlst. we run the loop through the element on nlst and keep adding all their element into the first number which is stored in sm. At the end just return sm .
mylist=[23,45,12,87,38]
def ListSum(lst):
sm,*nlst=lst
for i in nlst:
sm=sm+i
return sm
ListSum(mylist)
Use reduce() function :
In the below function we used reduce() function of python list to sum all the numbers in the list. We just passed list and a function which can be applied to that list which is add in this case.
from functools import reduce
from operator import add
mylist=[23,45,12,87,38]
def ListSum(lst):
sm=reduce(add,lst)
return sm
ListSum(mylist)
Use lambda() function :
In the below function we used reduce() function of python list to sum all the numbers in the list. We just passed list and a lambda() add function which can be applied to that list in this case.
from functools import reduce
mylist=[23,45,12,87,38]
def ListSum(lst):
s=reduce(lambda a,b:a+b,lst)
return s
ListSum(mylist)
Use recursive function :
In the below function we use recursive function to sum all the numbers in the list.
mylist=[23,45,12,87,38]
def ListSum(lst,sm=0):
a,*nlst=lst
sm=sm+a
if len(nlst)>0:
ListSum(nlst,sm)
else:
print(sm)
ListSum(mylist)
If you need to develop understanding on how to write your own function Contact us .
Want to Learn Python from our Expert . Check this out .
Don’t trust us , See if you like learnpython.com