This blog post will show you various ways to find the sum of even numbers in a Python list.
Find the sum of even numbers in a Python list
Table of Contents
- Problem
- Using modulus operator
%
to find if a number is even - Using bitwise operator
&
to find if a number is even - Finding the sum of even numbers using a
for
loop - Finding the sum of even numbers using list comprehension
- Finding the sum of even numbers using filter()
- Other ways: Using the ~ unary operator and recursion
- Conclusion
Problem
You are given a list like this:
nums = [2, 5, 3, 7, 5, 9, 5, 44, 3, 20, 21]
You have to write a program to find and print the sum of all the even numbers in the list nums
.
Using modulus operator %
to find if a number is even
Before the actual program, let us first see how to check if a number is an even number.
We can use the modulus operator %
for this. Modulus returns the remainder when a number is divided by 2. If the remainder is 0, it is an even number. Since we are interested in even numbers, we are interested in numbers where x % 2
returns 0 for number x
.
Start the Python shell and run this:
print(5 % 2)
The output will be 1, so it is an odd number.
print(8 % 2)
The output will be 0, so it is an odd number.
Using bitwise operator &
to find if a number is even
When a number is converted to its binary equivalent, for an even number, the last bit is always 0. When we perform the bitwise operation x AND 1
, if the last bit is 1, the result is 1, meaning the number is an odd number. Since we want even numbers, we are interested in x AND 1
returning 0.
In Python, the AND
operator is denoted by &
.
Run this on the Python shell:
print(5 & 1)
Output:
1
The output is 1, so it is an odd number
print(8 & 1)
Output:
0
The output is 0, so it is an even number
Finding the sum of even numbers using a for
loop
For someone who is new to Python, this program may be the easiest.
total = 0
for x in nums:
if x % 2 == 0:
total += x
print(total)
Here, we iterate through each number in the list nums
. Then, we check if each number is even, using the %
operator.
We can also use bitwise to check if the number is even:
total = 0
for x in nums:
if x & 1 == 0:
total += x
print(total)
Finding the sum of even numbers using list comprehension
Once you learn about list comprehension, it is more efficient to use it for problems like this.
This program finds the sum of even numbers using list comprehension.
Using modulus operator
total = [sum(x) for x in nums if x % 2 == 0]
print(total)
Using bitwise operator &
total = [sum(x) for x in nums if x & 1 == 0]
print(total)
Finding the sum of even numbers using filter()
Using filter()
and lambda
, we can find the sum like this:
total = sum(filter(lambda x: x % 2 == 0, nums))
print(total)
Here, filter()
includes only the numbers which make this expression x % 2 == 0
true.
Another way of using filter()
and lambda
total = sum(filter(lambda x: not x % 2, nums))
print(total)
It does the same thing. x % 2 == 0
is equivalent to not x % 2
.
Other ways: Using the ~ unary operator and recursion
There are other ways of doing this, using the unary operator ~
and recursion, but they are inefficient and more time consuming. So, we will stick to the methods above.
Conclusion
Have you used any of these methods in an interview or school assignment? Let me know what worked and didn't work for you in the comments below.
Related Posts
If you have any questions, please contact me at arulbOsutkNiqlzziyties@gNqmaizl.bkcom. You can also post questions in our Facebook group. Thank you.