The Wayback Machine - https://web.archive.org/web/20241004183857/https://www.geeksforgeeks.org/python-program-to-count-even-and-odd-numbers-in-a-list/
Open In App

Python program to count Even and Odd numbers in a List

Last Updated : 22 Apr, 2023
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Given a list of numbers, write a Python program to count Even and Odd numbers in a List.

Example:

Input: list1 = [2, 7, 5, 64, 14]
Output: Even = 3, odd = 2

Input: list2 = [12, 14, 95, 3]
Output: Even = 2, odd = 2

Example 1: 

Count Even and Odd numbers from the given list using for loop Iterate each element in the list using for loop and check if num % 2 == 0, the condition to check even numbers. If the condition satisfies, then increase the even count else increase odd count. 

Python3




# Python program to count Even
# and Odd numbers in a List
 
# list of numbers
list1 = [10, 21, 4, 45, 66, 93, 1]
 
even_count, odd_count = 0, 0
 
# iterating each number in list
for num in list1:
 
    # checking condition
    if num % 2 == 0:
        even_count += 1
 
    else:
        odd_count += 1
 
print("Even numbers in the list: ", even_count)
print("Odd numbers in the list: ", odd_count)


Output:

Even numbers in the list:  3
Odd numbers in the list:  4

Time Complexity: O(N), Here N is the number of elements in the list.
Auxiliary Space: O(1), As constant extra space is used.

Example 2: Using while loop 

Python3




# Python program to count Even and Odd numbers in a List
 
# list of numbers
list1 = [10, 21, 4, 45, 66, 93, 11]
 
even_count, odd_count = 0, 0
num = 0
 
# using while loop    
while(num < len(list1)):
     
    # checking condition
    if list1[num] % 2 == 0:
        even_count += 1
    else:
        odd_count += 1
     
    # increment num
    num += 1
     
print("Even numbers in the list: ", even_count)
print("Odd numbers in the list: ", odd_count)


Output:

Even numbers in the list:  3
Odd numbers in the list:  4

Time Complexity: O(N), Here N is the number of elements in the list.
Auxiliary Space: O(1), As constant extra space is used.

Example 3: Using Python Lambda Expressions 

Python3




# list of numbers
list1 = [10, 21, 4, 45, 66, 93, 11]
 
odd_count = len(list(filter(lambda x: (x%2 != 0) , list1)))
 
# we can also do len(list1) - odd_count
even_count = len(list(filter(lambda x: (x%2 == 0) , list1)))
 
print("Even numbers in the list: ", even_count)
print("Odd numbers in the list: ", odd_count)


Output:

Even numbers in the list:  3
Odd numbers in the list:  4

Time Complexity: O(n)
Space Complexity: O(1)

Example 4: Using List Comprehension 

Python3




# Python program to print odd Numbers in a List
 
# list of numbers
list1 = [10, 21, 4, 45, 66, 93, 11]
 
only_odd = [num for num in list1 if num % 2 == 1]
odd_count = len(only_odd)
 
print("Even numbers in the list: ", len(list1) - odd_count)
print("Odd numbers in the list: ", odd_count)


Output:

Even numbers in the list:  3
Odd numbers in the list:  4

Time Complexity: O(N), Here N is the number of elements in the list.
Auxiliary Space: O(1), As constant extra space is used.

Example 5: Using Recursion

Python3




# Python program to count Even
# and Odd numbers in a List
# using recursion
even_count = 0  # even counter
i = 0  # index, so that we can check if list[i] is even or odd
odd_count = 0  # odd counter
 
 
def evenoddcount(lst):
    # defining local counters as global variable
    global even_count
    global odd_count
    global i
    if lst[i] % 2 == 0:  # check if number is even
        even_count += 1
    else:  # if number is odd
        odd_count += 1
    if i in range(len(lst)-1):
        i += 1  # increment i
        evenoddcount(lst)  # calling fonction recursively
    else:
        print("Even numbers in the list: ", even_count)
        print("Odd numbers in the list: ", odd_count)
 
 
list1 = [10, 21, 4, 45, 66, 93, 1]
evenoddcount(list1)


Output

Even numbers in the list:  3
Odd numbers in the list:  4

Time Complexity: O(N), Here N is the number of elements in the list.
Auxiliary Space: O(1), As constant extra space is used.

Example 6: Using Bitwise XOR operator

The idea is to check whether the last bit of the number is set or not. If the last bit is set then the number is odd, otherwise even. As we know bitwise XOR Operation of the Number by 1 increments the value of the number by 1 if the number is even otherwise it decrements the value of the number by 1 if the value is odd.

CHECK IF NUMBER IS EVEN OR ODD USING XOR OPERATOR


Python3




# Python program to count Even
# and Odd numbers in a List
# using Bitwise XOR
 
# list of numbers
list1 = [10, 21, 4, 45, 66, 93, 1]
 
even_count, odd_count = 0, 0
for num in list1:
 
    # checking condition
    if num ^ 1 == num + 1:
        even_count += 1
 
    else:
        odd_count += 1
 
print("Even numbers in the list: ", even_count)
print("Odd numbers in the list: ", odd_count)
# This code is contributed by Shivesh Kumar Dwivedi


Output

Even numbers in the list:  3
Odd numbers in the list:  4

Time Complexity: O(N), Here N is the number of elements in the list.
Auxiliary Space: O(1), As constant extra space is used.

Example 7: Using Bitwise AND operator

The idea is to check whether the last bit of the number is set or not. If the last bit is set then the number is odd, otherwise even.
As we know bitwise AND Operation of the Number by 1 will be 1, If it is odd because the last bit will be already set. Otherwise, it will give 0 as output. 

Python3




# Python program to count Even
# and Odd numbers in a List
# using Bitwise AND
 
# list of numbers
list1 = [10, 21, 4, 45, 66, 93, 1]
 
even_count, odd_count = 0, 0
for num in list1:
 
    # checking condition
    if not num & 1:
        even_count += 1
 
    else:
        odd_count += 1
 
print("Even numbers in the list: ", even_count)
print("Odd numbers in the list: ", odd_count)


Output

Even numbers in the list:  3
Odd numbers in the list:  4

Time Complexity: O(N), Here N is the number of elements in the list.
Auxiliary Space: O(1), As constant extra space is used.

Example 8: Using Bitwise OR operator

The idea is to check whether the last bit of the number is set or not. If the last bit is set then the number is odd, otherwise even. As we know bitwise OR Operation of the Number by 1 increment the value of the number by 1 if the number is even otherwise it will remain unchanged. So, if after OR operation of number with 1 gives a result which is greater than the number then it is even and we will return true otherwise it is odd and we will return false.

Python3




# Python program to count Even
# and Odd numbers in a List
# using Bitwise OR
 
# list of numbers
list1 = [10, 21, 4, 45, 66, 93, 1]
 
even_count, odd_count = 0, 0
for num in list1:
       
    # checking condition
    if num | 1 > num:
        even_count += 1
   
    else:
        odd_count += 1
           
print("Even numbers in the list: ", even_count)
print("Odd numbers in the list: ", odd_count)
# This code is contributed by Shivesh Kumar Dwivedi


Output

Even numbers in the list:  3
Odd numbers in the list:  4

Time Complexity: O(N), Here N is the number of elements in the list.
Auxiliary Space: O(1), As constant extra space is used.

Method: Using the enumerate function 

Python3




lst = [12, 14, 95, 3];c=0;c1=0
for i,a in enumerate(lst):
    if a%2==0:
        c+=1
    else:
        c1+=1
     
print("even number count",c,"odd number count",c1)


Output

even number count 2 odd number count 2

Time Complexity: O(N), Here N is the number of elements in the list.
Auxiliary Space: O(1), As constant extra space is used.

Method: Using Numpy.Array : 

Python




# Python program to Print Positive Numbers in a List
import numpy as np
 
# list of numbers
List = [10, 21, 4, 45, 66, 93, 11]
 
# using numpy Array
list1 = np.array(List)
Even_list = list1[list1 % 2 == 0]
 
print("Even numbers in the list: ", len(Even_list))
print("Odd numbers in the list: ", len(list1)-len(Even_list))


Output:

Even numbers in the list:  3
Odd numbers in the list:  4

Time Complexity: O(N), Here N is the number of elements in the list.
Auxiliary Space: O(1), As constant extra space is used.

Method: Using Numpy.where() function : 

note: install numpy module using command “pip install numpy”

Algorithm:

Convert the given list to a numpy array.
Use numpy.where() function to find the indices of even and odd numbers in the array.
Count the number of indices using the len() function.
Print the counts.

Here’s the Python program to count Even and Odd numbers in a List using numpy.where() function:

Python3




import numpy as np
 
# list of numbers
list1 = [2, 7, 5, 64, 14]
 
# create numpy array from list
arr = np.array(list1)
 
# count even numbers
even_count = len(np.where(arr % 2 == 0)[0])
 
# count odd numbers
odd_count = len(np.where(arr % 2 == 1)[0])
 
# print counts
print("Even numbers in the list: ", even_count)
print("Odd numbers in the list: ", odd_count)


Output:

Even numbers in the list:  3
Odd numbers in the list:  2

Time Complexity: O(N), where N is the number of elements in the list.
Space Complexity: O(N), as we create a numpy array of size N.

Method: Using Sum and len function  

Approach: 

  • Initialize list. 
  • Initialize even_count and odd_count variables to store numbers. 
  • Use Sum method which counts 1 if the number in the list is odd and store the result in odd_count. 
  • Subtract the total length list to odd_count and store the value in even_count. 
  • Print event_count and odd_count. 

Python3




# Python program to count Even
# and Odd numbers in a List
 
# list of numbers
list1 = [10, 21, 4, 45, 66, 93, 1]
 
even_count, odd_count = 0, 0
 
# Using sum function
odd_count = sum(1 for i in list1 if i&1)
even_count = len(list1) - odd_count
 
print("Even numbers in the list: ", even_count)
print("Odd numbers in the list: ", odd_count)


Output:

Even numbers in the list:  3
Odd numbers in the list:  4

Time Complexity: O(n), where n is the length of the list.
Space Complexity: O(1), Because no extra space is used.



Previous Article
Next Article

Similar Reads

Count of numbers of length N having prime numbers at odd indices and odd numbers at even indices
Given a number N, the task is to calculate the count of numbers of length N having prime numbers at odd indices and odd numbers at even indices. Example: Input : N = 1Output: 5Explanation : All valid numbers length 1 are 1, 3, 5, 7, 9, here we have only 1 odd index, therefore we have 5 valid numbers. Input: N = 2Output: 20 Explanation: There are 20
5 min read
Even numbers at even index and odd numbers at odd index
Given an array of size n containing equal number of odd and even numbers. The problem is to arrange the numbers in such a way that all the even numbers get the even index and odd numbers get the odd index. Required auxiliary space is O(1).Examples : Input : arr[] = {3, 6, 12, 1, 5, 8} Output : 6 3 12 1 8 5 Input : arr[] = {10, 9, 7, 18, 13, 19, 4,
11 min read
Count numbers from given range having odd digits at odd places and even digits at even places
Given two integers L and R, the task is to count numbers from the range [L, R] having odd digits at odd positions and even digits at even positions respectively. Examples: Input: L = 3, R = 25Output: 9Explanation: The numbers satisfying the conditions are 3, 5, 7, 9, 10, 12, 14, 16 and 18. Input: L = 128, R = 162Output: 7Explanation: The numbers sa
15+ min read
Check if a number has an odd count of odd divisors and even count of even divisors
Given an integer N, the task is to check if N has an odd number of odd divisors and even number of even divisors. Examples: Input: N = 36Output: YesExplanation:Divisors of 36 = 1, 2, 3, 4, 6, 9, 12, 18, 36Count of Odd Divisors(1, 3, 9) = 3 [Odd]Count of Even Divisors(2, 4, 6, 12, 18, 36) = 6 [Even] Input: N = 28Output: No Naive Approach: The idea i
9 min read
Count of integers in a range which have even number of odd digits and odd number of even digits
Given a range [L, R], the task is to count the numbers which have even number of odd digits and odd number of even digits. For example, 8 has 1 even digit and 0 odd digit - Satisfies the condition since 1 is odd and 0 is even.545 has 1 even digit and 2 odd digits - Satisfies the condition since 1 is odd and 2 is even.4834 has 3 even digits and 1 od
11 min read
Find the nearest odd and even perfect squares of odd and even array elements respectively
Given an array arr[ ] of size N, the task for each array element is to print the nearest perfect square having same parity. Examples: Input: arr[ ] = {6, 3, 2, 15}Output: 4 1 4 9Explanation:The nearest even perfect square of arr[0] (= 6) is 4.The nearest odd perfect square of arr[1] (= 3) is 1.The nearest even perfect square of arr[2] (= 2) is 4The
5 min read
Count N-digits numbers made up of even and prime digits at odd and even positions respectively
Given a positive integer N, the task is to find the number of integers of N digits having even digits at odd indices and prime digits at even indices. Examples: Input: N = 2Output: 20Explanation:Following are the possible number of 2-digits satisfying the given criteria {20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 50, 52, 54, 56, 58, 70, 72, 74, 76, 78
12 min read
Modify Binary Tree by replacing all nodes at even and odd levels by their nearest even or odd perfect squares respectively
Given a Binary Tree consisting of N nodes, the task is to replace all the nodes that are present at even-numbered levels in a Binary Tree with their nearest even perfect square and replace nodes at odd-numbered levels with their nearest odd perfect square. Examples: Input: 5 / \ 3 2 / \ 16 19 Output: 9 / \ 4 4 / \ 9 25 Explanation: Level 1: Nearest
13 min read
Absolute difference between sum of even elements at even indices & odd elements at odd indices in given Array
Given an array arr[] containing N elements, the task is to find the absolute difference between the sum of even elements at even indices & the count of odd elements at odd indices. Consider 1-based indexing Examples: Input: arr[] = {3, 4, 1, 5}Output: 0Explanation: Sum of even elements at even indices: 4 {4}Sum of odd elements at odd indices: 4
5 min read
Python program to count Even and Odd numbers in a Dictionary
Given a python dictionary, the task is to count even and odd numbers present in the dictionary. Examples: Input : {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e' : 5}Output : Even = 2, odd = 3Input : {'x': 4, 'y':9, 'z':16}Output : Even = 2, odd = 1 Approach using values() Function: Traverse the dictionary and extract its elements using values() function and
3 min read
Python Program to find Sum of Negative, Positive Even and Positive Odd numbers in a List
Given a list. The task is to find the sum of Negative, Positive Even, and Positive Odd numbers present in the List. Examples: Input: -7 5 60 -34 1 Output: Sum of negative numbers is -41 Sum of even positive numbers is 60 Sum of odd positive numbers is 6 Input: 1 -1 50 -2 0 -3 Output: Sum of negative numbers is -6 Sum of even positive numbers is 50
7 min read
Count of N-digit Numbers having Sum of even and odd positioned digits divisible by given numbers
Given integers A, B, and N, the task is to find the total number of N-digit numbers whose sum of digits at even positions and odd positions are divisible by A and B respectively. Examples: Input: N = 2, A = 2, B = 5Output: 5Explanation:The only 2-digit numbers {50, 52, 54, 56, 58} with sum of odd-positioned digits equal to 5 and even-positioned dig
15+ min read
Count of N-digit numbers having equal count of distinct odd and even digits
Given a positive integer N, the task is to count the number of N-digit numbers such that the count of distinct odd and distinct even digits in the number is the same. Examples: Input: N = 2Output : 45Explanation:For a 2-digit number, in order to satisfy the condition, the first digit can be even and second digit odd, or the second digit can be odd
15 min read
Kth element in permutation of first N natural numbers having all even numbers placed before odd numbers in increasing order
Given two integers N and K, the task is to find the Kth element in the permutation of first N natural numbers arranged such that all the even numbers appear before the odd numbers in increasing order. Examples : Input: N = 10, K = 3 Output: 6Explanation:The required permutation is {2, 4, 6, 8, 10, 1, 3, 5, 7, 9}.The 3rd number in the permutation is
9 min read
Javascript Program to Rotate all odd numbers right and all even numbers left in an Array of 1 to N
Given a permutation arrays A[] consisting of N numbers in range [1, N], the task is to left rotate all the even numbers and right rotate all the odd numbers of the permutation and print the updated permutation. Note: N is always even.Examples:  Input: A = {1, 2, 3, 4, 5, 6, 7, 8} Output: {7, 4, 1, 6, 3, 8, 5, 2} Explanation: Even element = {2, 4, 6
2 min read
Python3 Program to Rotate all odd numbers right and all even numbers left in an Array of 1 to N
Given a permutation arrays A[] consisting of N numbers in range [1, N], the task is to left rotate all the even numbers and right rotate all the odd numbers of the permutation and print the updated permutation. Note: N is always even.Examples: Input: A = {1, 2, 3, 4, 5, 6, 7, 8} Output: {7, 4, 1, 6, 3, 8, 5, 2} Explanation: Even element = {2, 4, 6,
2 min read
Python Program for Frequencies of even and odd numbers in a matrix
Given a matrix of order m*n then the task is to find the frequency of even and odd numbers in matrix Examples: Input : m = 3, n = 3 { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } Output : Frequency of odd number = 5 Frequency of even number = 4 Input : m = 3, n = 3 { 10, 11, 12 }, { 13, 14, 15 }, { 16, 17, 18 } Output : Frequency of odd number = 4 Frequency
4 min read
Generate an array of given size with equal count and sum of odd and even numbers
Given an integer N, the task is to find an array of length N that contains same count of odd and even elements with an equal sum of even and odd elements in the array.Note: Print -1 if no such array is possible. Examples: Input: N = 4 Output: 1 2 5 4 Explanation: Even elements of the array - {2, 4}, S(even) = 6 Odd elements of the array - {1, 5}, S
8 min read
Sort all even numbers in ascending order and then sort all odd numbers in descending order
Given an array of integers (both odd and even), sort them in such a way that the first part of the array contains odd numbers sorted in descending order, rest portion contains even numbers sorted in ascending order.Examples: Input: arr[] = {1, 2, 3, 5, 4, 7, 10}Output: arr[] = {7, 5, 3, 1, 2, 4, 10} Input: arr[] = {0, 4, 5, 3, 7, 2, 1}Output: arr[]
15+ min read
Python program to find the sum of all even and odd digits of an integer list
The following article shows how given an integer list, we can produce the sum of all its odd and even digits. Input : test_list = [345, 893, 1948, 34, 2346] Output : Odd digit sum : 36 Even digit sum : 40 Explanation : 3 + 5 + 9 + 3 + 1 + 9 + 3 + 3 = 36, odd summation.Input : test_list = [345, 893] Output : Odd digit sum : 20 Even digit sum : 12 Ex
5 min read
Python Program to Print Largest Even and Largest Odd Number in a List
Auxiliary Given a list. The task is to print the largest even and largest odd number in a list. Examples: Input: 1 3 5 8 6 10 Output: Largest even number is 10 Largest odd number is 5 Input: 123 234 236 694 809 Output: Largest odd number is 809 Largest even number is 694 The first approach uses two methods , one for computing largest even number an
7 min read
Python Program For Arranging Single Linked List In Alternate Odd and Even Nodes Order
Given a singly linked list, rearrange the list so that even and odd nodes are alternate in the list.There are two possible forms of this rearrangement. If the first data is odd, then the second node must be even. The third node must be odd and so on. Notice that another arrangement is possible where the first node is even, second odd, third even an
7 min read
Python Program For Rearranging A Linked List Such That All Even And Odd Positioned Nodes Are Together
Rearrange a linked list in such a way that all odd position nodes are together and all even positions node are together, Examples: Input: 1->2->3->4 Output: 1->3->2->4 Input: 10->22->30->43->56->70 Output: 10->30->56->22->43->70Recommended: Please solve it on "PRACTICE" first, before moving on to the
6 min read
Maximize the difference of sum of elements at even indices and odd indices by shifting an odd sized subarray to end of given Array.
Given an array arr[] of size N, the task is to maximize the difference of the sum of elements at even indices and elements at odd indices by shifting any subarray of odd length to the end of the array. Examples: Input: arr[] = {1, 2, 3, 4, 5, 6}Output: 3Explanation: Initially sum of elements at even indices = 1 + 3 + 5 = 9Sum of elements at odd ind
13 min read
Count Odd and Even numbers in a range from L to R
Given two numbers L and R, the task is to count the number of odd and even numbers in the range L to R.Examples: Input: l = 3, r = 7 Output: 3 2 Count of odd numbers is 3 i.e. 3, 5, 7 Count of even numbers is 2 i.e. 4, 6Input: l = 4, r = 8 Output: 2 3Count of odd numbers is 2 i.e. 5, 7 Count of even numbers is 3 i.e. 4, 6, 8 Brute Force Approach: w
7 min read
Count Numbers in Range with difference between Sum of digits at even and odd positions as Prime
Given a range [L, R]. The task is to count the numbers in the range having difference between the sum of digits at even position and sum of digits at odd position is a Prime Number. Consider the position of least significant digit in the number as an odd position.Examples: Input : L = 1, R = 50 Output : 6 Explanation : Only, 20, 30, 31, 41, 42 and
12 min read
Count set bits in the Kth number after segregating even and odd from N natural numbers
Given two integers N and K, the task is to find the count of set bits in the Kth number in the Odd-Even sequence made of the number from the range [1, N]. The Odd-Even sequence first contains all the odd numbers from 1 to N and then all the even numbers from 1 to N.Examples: Input: N = 8, K = 4 Output: 3 The sequence is 1, 3, 5, 7, 2, 4, 6 and 8. 4
6 min read
Count even and odd Bitwise XORs of consecutive numbers in a range [L, R] starting from L
Given two integers L and R, the task is to find the count of even and odd Bitwise XOR values of consecutive numbers from the range [L, R] starting from L. Examples: Input: L = 2, R = 7Output: Even = 3, Odd = 3Explanation: Taking bitwise XOR of continuous numbers:22 ^ 3 = 12 ^ 3 ^ 4 = 52 ^ 3 ^ 4 ^ 5 = 02 ^ 3 ^ 4 ^ 5 ^ 6 = 62 ^ 3 ^ 4 ^ 5 ^ 6 ^ 7 = 1T
7 min read
Count total number of N digit numbers such that the difference between sum of even and odd digits is 1
Given a number n, we need to count the total number of n digit numbers such that the sum of even digits is 1 more than the sum of odd digits. Here even and odd means positions of digits are like array indexes, for example, the leftmost (or leading) digit is considered as even digit, next to leftmost is considered as odd, and so on. Example: Input:
10 min read
Python Program for Check if count of divisors is even or odd
Write a Python program for a given number “n”, the task is to find its total number of divisors that are even or odd. Examples: Input : n = 10 Output: Even Input: n = 100Output: Odd Input: n = 125Output: Even Python Program for Check if count of divisors is even or odd using Naive Approach:A naive approach would be to find all the divisors and then
4 min read