Python program to find the sum of all even and odd digits of an integer list
Last Updated :
04 Apr, 2023
The following article shows how given an integer list, we can produce the sum of all its odd and even digits.
Input : test_list = [345, 893, 1948, 34, 2346]
Output :
Odd digit sum : 36
Even digit sum : 40
Explanation : 3 + 5 + 9 + 3 + 1 + 9 + 3 + 3 = 36, odd summation.
Input : test_list = [345, 893]
Output :
Odd digit sum : 20
Even digit sum : 12
Explanation : 4 + 8 = 12, even summation.
Method 1 : Using loop, str() and int()
In this, we first convert each element to string and then iterate for each of its element, and add to respective summation by conversion to integer.
Python3
# initializing list
test_list = [345, 893, 1948, 34, 2346]
# printing original list
print("The original list is : " + str(test_list))
odd_sum = 0
even_sum = 0
for sub in test_list:
for ele in str(sub):
# adding in particular summation according to value
if int(ele) % 2 == 0:
even_sum += int(ele)
else:
odd_sum += int(ele)
# printing result
print("Odd digit sum : " + str(odd_sum))
print("Even digit sum : " + str(even_sum))
OutputThe original list is : [345, 893, 1948, 34, 2346]
Odd digit sum : 36
Even digit sum : 40
Time Complexity: O(n*n) where n is the length of the input list test_list, and m is the maximum number of digits in any element of the list.
Auxiliary Space:The auxiliary space complexity of this program is O(1), because the program uses a constant amount of extra space to store the odd_sum and even_sum variables, regardless of the size of the input list.
Method 2: Using loop and sum()
In this, we perform task of getting summation using sum(), and loop is used to perform the task of iterating through each element.
Python3
# initializing list
test_list = [345, 893, 1948, 34, 2346]
# printing original list
print("The original list is : " + str(test_list))
odd_sum = 0
even_sum = 0
for sub in test_list:
# sum() used to get summation of even and odd elements
odd_sum += sum([int(ele) for ele in str(sub) if int(ele) % 2 == 1])
even_sum += sum([int(ele) for ele in str(sub) if int(ele) % 2 == 0])
# printing result
print("Odd digit sum : " + str(odd_sum))
print("Even digit sum : " + str(even_sum))
OutputThe original list is : [345, 893, 1948, 34, 2346]
Odd digit sum : 36
Even digit sum : 40
Time complexity: O(N * M).
Auxiliary space: O(1).
Method 3: Using list comprehension
Python3
test_list = [345, 893, 1948, 34, 2346]
odd_sum = 0
even_sum = 0
odd_sum += sum([int(ele)
for sub in test_list for ele in str(sub) if int(ele) % 2 == 1])
even_sum += sum([int(ele)
for sub in test_list for ele in str(sub) if int(ele) % 2 == 0])
print("Odd digit sum : " + str(odd_sum))
print("Even digit sum : " + str(even_sum))
OutputOdd digit sum : 36
Even digit sum : 40
Time complexity: O(nm).
Auxiliary space: O(1).
Method 4: Using the enumerate function
Python3
test_list = [345, 893, 1948, 34, 2346]
odd_sum = 0
even_sum = 0
odd_sum += sum([int(ele) for i, sub in enumerate(test_list)
for ele in str(sub) if int(ele) % 2 == 1])
even_sum += sum([int(ele) for i, sub in enumerate(test_list)
for ele in str(sub) if int(ele) % 2 == 0])
print("Odd digit sum : " + str(odd_sum))
print("Even digit sum : " + str(even_sum))
OutputOdd digit sum : 36
Even digit sum : 40
The time complexity of the given program is O(n*k), where n is the number of elements in the list and k is the maximum number of digits in any element of the list.
The space complexity of the given program is O(1) because it uses a constant amount of extra space regardless of the size of the input.
Method 5: Using recursion
Python3
def digit_sum(sub, even_sum, odd_sum):
if not sub:
return (even_sum, odd_sum)
else:
ele = sub.pop()
if int(ele) % 2 == 0:
even_sum += int(ele)
else:
odd_sum += int(ele)
return digit_sum(sub, even_sum, odd_sum)
test_list = [345, 893, 1948, 34, 2346]
# printing original list
print("The original list is : " + str(test_list))
even_sum = 0
odd_sum = 0
for sub in test_list:
sub = list(str(sub))
even_sum, odd_sum = digit_sum(sub, even_sum, odd_sum)
# printing result
print("Odd digit sum : " + str(odd_sum))
print("Even digit sum : " + str(even_sum))
#This article is published by Vinay Pinjala.
OutputThe original list is : [345, 893, 1948, 34, 2346]
Odd digit sum : 36
Even digit sum : 40
Time Complexity: O(n)
Auxiliary Space: O(n)
Approach: Iterative Digit Summing Approach
Steps:
- Initialize two variables, even_sum and odd_sum, to 0.
- Iterate over each number in the list using a while loop until the number becomes 0.
- Extract the last digit of the number using modulo operator and add it to even_sum if it is even, else add it to odd_sum.
- Remove the last digit from the number using integer division operator.
- Return both even_sum and odd_sum.
Python3
def sum_of_even_odd_digits(test_list):
even_sum = 0
odd_sum = 0
for num in test_list:
while num != 0:
digit = num % 10
if digit % 2 == 0:
even_sum += digit
else:
odd_sum += digit
num //= 10
return even_sum, odd_sum
test_list = [345, 893, 1948, 34, 2346]
even_sum, odd_sum = sum_of_even_odd_digits(test_list)
print("Odd digit sum :", odd_sum)
print("Even digit sum :", even_sum)
OutputOdd digit sum : 36
Even digit sum : 40
Time Complexity: O(n * d), where n is the number of elements in the list and d is the maximum number of digits in any number in the list.
Auxiliary Space: O(1)
Similar Reads
Python Program to Find Sum of First and Last Digit Given a positive integer N(at least contain two digits). The task is to write a Python program to add the first and last digits of the given number N. Examples: Input: N = 1247 Output: 8 Explanation: First digit is 1 and Last digit is 7. So, addition of these two (1 + 7) is equal to 8.Input: N = 73
5 min read
Python program to find sum of elements in list Finding the sum of elements in a list means adding all the values together to get a single total. For example, given a list like [10, 20, 30, 40, 50], you might want to calculate the total sum, which is 150. Let's explore these different methods to do this efficiently.Using sum()sum() function is th
3 min read
Python Program to Get Sum of cubes of alternate even numbers in an array Given an array, write a program to find the sum of cubes of alternative even numbers in an array.Examples:Input : arr = {1, 2, 3, 4, 5, 6}Output : Even elements in given array are2,4,6Sum of cube of alternate even numbers are 2**3+6**3 = 224Input : arr = {1,3,5,8,10,9,11,12,1,14}Output : Even elemen
5 min read
Python Program to find Sum of Negative, Positive Even and Positive Odd numbers in a List Given a list. The task is to find the sum of Negative, Positive Even, and Positive Odd numbers present in the List. Examples: Input: -7 5 60 -34 1 Output: Sum of negative numbers is -41 Sum of even positive numbers is 60 Sum of odd positive numbers is 6 Input: 1 -1 50 -2 0 -3 Output: Sum of negative
7 min read
Python Program to Split the Even and Odd elements into two different lists In Python, it's a common task to separate even and odd numbers from a given list into two different lists. This problem can be solved using various methods. In this article, weâll explore the most efficient ways to split even and odd elements into two separate lists.Using List ComprehensionList comp
3 min read