Python program to count Even and Odd numbers in a List
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 = 2Input: 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) |
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) |
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) |
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) |
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) |
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.
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 |
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) |
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 |
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) |
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.
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.