
- Python 3 Basic Tutorial
- Python 3 - Home
- What is New in Python 3
- Python 3 - Overview
- Python 3 - Environment Setup
- Python 3 - Basic Syntax
- Python 3 - Variable Types
- Python 3 - Basic Operators
- Python 3 - Decision Making
- Python 3 - Loops
- Python 3 - Numbers
- Python 3 - Strings
- Python 3 - Lists
- Python 3 - Tuples
- Python 3 - Dictionary
- Python 3 - Date & Time
- Python 3 - Functions
- Python 3 - Modules
- Python 3 - Files I/O
- Python 3 - Exceptions
Python program to Count Even and Odd numbers in a List
In this article, we will learn about the solution to the problem statement given below.
Problem statement − We are given a list, we need to count even and odd numbers in a list.
There are three methods as discussed below−
Approach 1 − Using brute-force approach
Example
list1 = [21,3,4,6,33,2,3,1,3,76] even_count, odd_count = 0, 0 # enhanced for loop for num in list1: #even numbers if num % 2 == 0: even_count += 1 #odd numbers else: odd_count += 1 print("Even numbers available in the list: ", even_count) print("Odd numbers available in the list: ", odd_count)
Output
Even numbers available in the list: 4 Odd numbers available in the list: 6
Approach 2 − Using filter() and lambda expressions
Example
list1 = [21,3,4,6,33,2,3,1,3,76] #odd numbers odd_count = len(list(filter(lambda x: (x%2 != 0) , list1))) #even numbers even_count = len(list(filter(lambda x: (x%2 == 0) , list1))) print("Even numbers available in the list: ", even_count) print("Odd numbers available in the list: ", odd_count)
Output
Even numbers available in the list: 4 Odd numbers available in the list: 6
Approach 3 − Using the list comprehension
Example
list1 = [21,3,4,6,33,2,3,1,3,76] #copy of list elements which are odd in a new list and calculating the length on new list only_odd = [num for num in list1 if num % 2 == 1] odd_count = len(only_odd) print("Even numbers available in the list: ", len(list1) - odd_count) print("Odd numbers available in the list: ", odd_count)
Output
Even numbers available in the list: 4 Odd numbers available in the list: 6
Conclusion
In this article, we have learned how to count even and odd numbers in a list.
- Related Articles
- Python program to print odd numbers in a list
- Python program to print even numbers in a list
- Count Odd and Even numbers in a range from L to R in C++
- Odd Even Linked List in Python
- Python Program to Put Even and Odd elements in a List into Two Different Lists
- Python program to count positive and negative numbers in a list
- Even numbers at even index and odd numbers at odd index in C++
- Program to sort all even and odd numbers in increasing and decreasing order respectively in Python
- Program to count odd numbers in an interval range using Python
- Count positive and negative numbers in a list in Python program
- Program to count number of valid pairs from a list of numbers, where pair sum is odd in Python
- Golang Program to Print the Largest Even and Largest Odd Number in a List
- Write a Golang program to find odd and even numbers using bit operation
- What are even and odd numbers?
- C program to store even, odd and prime numbers into separate files

Advertisements