Open In App

Python program to count Even and Odd numbers in a List

Last Updated : 22 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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 &amp; 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