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
Terrific article! This is the type of info that are
supposed to be shared across the net. Disgrace on the search engines for
now not positioning this post higher! Come on over and visit my web
site . Thank you =)
Hello! I’m at work surfing around your blog from my new iphone 3gs!
Just wanted to say I love reading your blog and look forward to all your posts!
Carry on the superb work!
My web page; youtube converter
Gran aporte. Es un tema relevante que merece más discusión.
You actually make it seem so easy with your presentation but
I find this matter to be really something that
I think I would never understand. It seems too complicated
and very broad for me. I’m looking forward for your next post, I’ll try to get the hang of it!
昔から憧れてた、こんなたくさん旅できたらな。やる気もらえます!。 [url=https://iqvel.com/ja/a/%E3%82%AB%E3%83%8A%E3%83%80/%E3%82%BB%E3%83%BC%E3%83%96%E3%83%AB%E5%B3%B6]霧と難破船[/url] 倫理意識高い — これが未来の旅。
Great article.
Wonderful site you have here but I was curious about if you knew
of any forums that cover the same topics discussed in this article?
I’d really love to be a part of group where I can get feed-back from other knowledgeable
individuals that share the same interest. If you have
any suggestions, please let me know. Thanks!
Thanks designed for sharing such a pleasant opinion, post is fastidious, thats why i
have read it completely
We’re a bunch of volunteers and starting a brand new scheme in our community.
Your web site provided us with helpful information to work
on. You’ve done a formidable job and our whole community might be grateful to you.
Good way of telling, and pleasant article to obtain facts regarding my presentation focus, which i am going to deliver in school.