Sum of digits in python using lambda

How can I convert these two functions to use lambda notation?

def sum_digits(number):
    if number == 0:
        return 0
    else:
        return (number % 10) + sum_digits(number / 10)

def count_digit(number):
    if number == 0:
        return 0
    else:
        return 1 + count_digit(number/10)

asked Apr 9, 2017 at 2:30

Sum of digits in python using lambda

1

sum_digits = lambda number: 0 if number == 0 else (number % 10) + sum_digits (number / 10)

count_digit = lambda number: 0 if number == 0 else 1 + count_digit(number/10)

Incidentally, this is a bad time to use lambdas, since you need the function names in order for them to call themselves. The point of lambdas is that they're anonymous.

answered Apr 9, 2017 at 2:35

Sum of digits in python using lambda

Arya McCarthyArya McCarthy

8,1733 gold badges31 silver badges53 bronze badges

5

Use a conditional expression for the body of the lambda:

>>> sum_digits = lambda n: 0 if n == 0 else (n % 10) + sum_digits(n // 10)
>>> count_digit = lambda n: 0 if n == 0 else 1 + count_digit(n // 10)

Also, if is preferred to use // for the division so that the code will still work in Python 3.

answered Apr 9, 2017 at 2:35

Raymond HettingerRaymond Hettinger

207k62 gold badges363 silver badges468 bronze badges

0

A string-oriented approach wouldn't require recursion:

sum_of_digits = lambda n: sum(int(d) for d in str(n))
count_digit = lambda n: len(str(n))

answered Nov 12, 2018 at 7:41

Sum of digits in python using lambda

martineaumartineau

115k25 gold badges160 silver badges284 bronze badges

sum_digits = lambda number: 0 if number == 0 else (number % 10) + sum_digits (number//10)
count_digit = lambda number: 0 if number == 0 else 1 + count_digit(number//10)
print(sum_digits(2546))
print(count_digit(2546))

Will work on python 3 too...

answered Apr 9, 2017 at 12:39

Sum of digits in python using lambda

print(list(map(lambda x: sum(int(i) for i in str(x)),list(map(int,input().split())))))

this might be useful for reading and using lamda function on the same line.

Yunnosch

25.4k9 gold badges40 silver badges52 bronze badges

answered Apr 15, 2021 at 5:35

Sum of digits in python using lambda

Last update on August 19 2022 21:50:46 (UTC/GMT +8 hours)

Python Lambda: Exercise-23 with Solution

Write a Python program to calculate the sum of the positive and negative numbers of a given list of numbers using lambda function.

Sample Solution:

Python Code :

nums = [2, 4, -6, -9, 11, -12, 14, -5, 17]
print("Original list:",nums)

total_negative_nums = list(filter(lambda nums:nums<0,nums))
total_positive_nums = list(filter(lambda nums:nums>0,nums))

print("Sum of the positive numbers: ",sum(total_negative_nums))
print("Sum of the negative numbers: ",sum(total_positive_nums))

Sample Output:

Original list: [2, 4, -6, -9, 11, -12, 14, -5, 17]
Sum of the positive numbers:  -32
Sum of the negative numbers:  48

Visualize Python code execution:

The following tool visualize what the computer is doing step-by-step as it executes the said program:

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python program that sum the length of the names of a given list of names after removing the names that starts with an lowercase letter. Use lambda function.
Next: Write a Python program to find numbers within a given range where every number is divisible by every digit it contains.

Python: Tips of the Day

Creating a sequence of numbers (zero to ten with skips):

>>> range(0,10,2)
[0, 2, 4, 6, 8]  

How do you sum a list of a digit in python?

See the code below:.
def sum_of_list(l): total = 0. for val in l: total = total + val. return total. ​ my_list = [1,3,5,2,4] ... .
def sum_of_list(l,n): if n == 0: return l[n]; return l[n] + sum_of_list(l,n-1) ​ my_list = [1,3,5,2,4] ... .
my_list = [1,3,5,2,4] print "The sum of my_list is", sum(my_list) Run..

How do you add two numbers using lambda expression?

Java Lambda Expression Example: with or without return keyword.
interface Addable{.
int add(int a,int b);.
public class LambdaExpressionExample6 {.
public static void main(String[] args) {.
// Lambda expression without return keyword..
Addable ad1=(a,b)->(a+b);.
System. out. println(ad1. add(10,20));.

What is Lamda function in python?

A lambda function is a small anonymous function. A lambda function can take any number of arguments, but can only have one expression.