Find sum of odd numbers of list in python by R4R Team

Example-

list is l=[1,2,1,2,1,2]
then sum of odd number from the list is 1+1+1=3


program-

#import the functools module
import functools

l=[1,2,3,4,5,6]
print("List is ",l)

#first way
sum=0
for i in l:
if i%2==1:
sum+=i
print("Sum of odd term is :",sum)

#second way
l=filter(lambda x: x%2==1,l )
sum=functools.reduce(lambda x,y:x+y,l)
print("Sum of odd term is :",sum)


output-

List is [1, 2, 3, 4, 5, 6]
Sum of odd term is : 9
Sum of odd term is : 9


-In this program, we have a list [1,2,3,4,5,6], then we find the sum of odd number from the list.




Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!