Python program to find the sum of all even and odd digits of an integer list

When it is required to find the sum of all even and odd digits of an integer list, a simple iteration and the modulus operator are used.

Below is a demonstration of the same ?

Example

my_list = [369, 793, 2848, 4314, 57467]

print("The list is :")
print(my_list)

sum_odd = 0
sum_even = 0

for index in my_list:
    for element in str(index):
        
        if int(element) % 2 == 0:
            sum_even += int(element)
        else:
            sum_odd += int(element)

print("The result is :")
print("The sum of odd digits is :")
print(sum_odd)
print("The sum of even digits is :")
print(sum_even)

Output

The list is :
[369, 793, 2848, 4314, 57467]
The result is :
The sum of odd digits is :
54
The sum of even digits is :
46

How It Works

The program uses a nested loop approach ?

  • The outer loop iterates through each integer in the list

  • The inner loop converts each integer to a string to access individual digits

  • Each digit is converted back to integer and checked using modulus operator (%)

  • If digit % 2 == 0, it's even; otherwise, it's odd

  • The digits are added to their respective sum variables

Alternative Approach Using List Comprehension

numbers = [369, 793, 2848, 4314, 57467]

# Extract all digits from all numbers
all_digits = [int(digit) for num in numbers for digit in str(num)]

sum_even = sum(digit for digit in all_digits if digit % 2 == 0)
sum_odd = sum(digit for digit in all_digits if digit % 2 != 0)

print(f"All digits: {all_digits}")
print(f"Sum of even digits: {sum_even}")
print(f"Sum of odd digits: {sum_odd}")
All digits: [3, 6, 9, 7, 9, 3, 2, 8, 4, 8, 4, 3, 1, 4, 5, 7, 4, 6, 7]
Sum of even digits: 46
Sum of odd digits: 54

Conclusion

Use nested loops to iterate through numbers and their digits. The modulus operator efficiently separates even and odd digits for separate summation.

Updated on: 2026-03-26T00:57:39+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements