• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • VBA
  • About
You are here: Home / Python / Using Python to Count Even Numbers in List

Using Python to Count Even Numbers in List

June 28, 2022 Leave a Comment

To count the even numbers in a list in Python, the easiest way is with list comprehension and the Python len() function.

lst = [0, 4, 6, 9, 2, 3, 1]

count = len([num for num in lst if num % 2 == 0])

print(count)

#Output:
4

You can also use a loop to count the number of even numbers in a list in Python.

lst = [0, 4, 6, 9, 2, 3, 1]

def countEvens(l):
    count = 0
    for num in l:
        if num % 2 == 0:
            count = count + 1
    return count

print(countEvens(lst))

#Output:
4

When working with collections of data, the ability to easily summarize and get statistics about the collection is valuable.

One such case is if you want to count the even numbers in a list.

To count the even numbers in a list in Python, the easiest way is with list comprehension and the Python len() function. To get the even numbers, we just need to check if the number is even or odd.

Below is a simple example showing you how to count the number of even numbers in a list using Python.

lst = [0, 4, 6, 9, 2, 3, 1]

count = len([num for num in lst if num % 2 == 0])

print(count)

#Output:
4

Finding Sum of Even Numbers Using sum() in Python

You can use other functions to summarize collections of data in Python just like with length.

One example is if you want to find the sum of the even numbers of a list.

In this case, you can use the Python sum() function.

Below is an example showing you how to sum the even numbers of a list using Python.

lst = [0, 4, 6, 9, 2, 3, 1]

s = sum([num for num in lst if num % 2 == 0])

print(s)

#Output:
12

Get Count of Odd Numbers in List Using Python

If you want to go the other way and get the count of odd numbers in a list using Python, you can just make a simple adjustment to the code above.

When using % to check if the number is even or odd, for odd numbers we want inequality.

Below is an example showing you how to count the odd numbers in a list using Python.

lst = [0, 4, 6, 9, 2, 3, 1]

count = len([num for num in lst if num % 2 != 0])

print(count)

#Output:
3

Hopefully this article has been useful for you to learn how to count the even numbers in a list using Python.

Other Articles You'll Also Like:

  • 1.  Write Integer to File Using Python
  • 2.  Divide Each Element in List by Scalar Value with Python
  • 3.  pandas ceil – Find the Ceiling of a Column Using Numpy ceil
  • 4.  PROC REG Equivalent in Python
  • 5.  Get Difference Between datetime Variables in Python
  • 6.  Are Tuples Mutable in Python? No, Tuples are not Mutable
  • 7.  Remove Extension from Filename in Python
  • 8.  Using Python to Compare Strings Alphabetically
  • 9.  Log Base 10 Python – Find Logarithm of Number with Base 10 with log10()
  • 10.  Transpose DataFrame pandas – Using the pandas transpose Function

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2022 · The Programming Expert · About · Privacy Policy