VOOZH about

URL: https://www.geeksforgeeks.org/python/python-program-to-count-even-and-odd-numbers-in-a-list/

⇱ Python Program to Count Even and Odd Numbers in a List - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Python Program to Count Even and Odd Numbers in a List

Last Updated : 11 Jul, 2025

In Python working with lists is a common task and one of the frequent operations is counting how many even and odd numbers are present in a given list. The collections.Counter method is the most efficient for large datasets, followed by the filter() and lambda approach for clean and compact code.

Using a Simple Loop

This method is very straightforward and easy to understand. It loops through the list and checks if each number is even or odd, and increments the respective counters.


Output
Even numbers: 4
Odd numbers: 5
  • Explanation: This approach simply iterates through the list, checks for even or odd, and updates the respective counters based on the condition.

Using collections.Counter

This method is highly efficient for larger lists and utilizes Python's built-in Counter from the collections module. The Counter helps to count the occurrence of each label.


Output
Even numbers: 4
Odd numbers: 5
  • Explanation: The Counter helps count occurrences of 'even' and 'odd' labels created using a list comprehension. This approach is very fast for large datasets.

Other methods that we can use to count even and odd numbers in a list are:

Using filter() and lambda

This method is efficient and concise, using filter() with lambda functions to separate even and odd numbers.It creates two lists: One for even numbers and another for odd numbers, the counts the length of these lists.


Output
Even numbers: 4
Odd numbers: 5
  • Explanation: The filter() function is used to extract even and odd numbers separately from the list. The length of the filtered lists gives the count of even and odd numbers.

Using a List Comprehension

List comprehension is a compact and efficient way to count even and odd numbers. This approach is ideal for smaller lists but can also be used for larger ones.


Output
Even numbers: 4
Odd numbers: 5
  • Explanation: List comprehension efficiently filters the list to create a new list of even and odd numbers, and the len() function gives the count.

Using Bitwise operator - Bonus Method

We can use Bitwise XOR with 1 to check the least significant bit (LSB). The LSB of an integer determines if it is even (0) or odd (1).


Output
Even count: 4
Odd count: 5


Comment